0% found this document useful (0 votes)
35 views10 pages

Pps Unit 2 Notes

The document provides an overview of operators in the C programming language, detailing types such as arithmetic, logical, relational, bitwise, assignment, and miscellaneous operators. It explains operator precedence and control statements, including conditional statements like if, if-else, nested if, and switch-case. Additionally, it introduces the conditional (ternary) operator as a concise alternative for simple if statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views10 pages

Pps Unit 2 Notes

The document provides an overview of operators in the C programming language, detailing types such as arithmetic, logical, relational, bitwise, assignment, and miscellaneous operators. It explains operator precedence and control statements, including conditional statements like if, if-else, nested if, and switch-case. Additionally, it introduces the conditional (ternary) operator as a concise alternative for simple if statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

qwertyuiopasdfghjklzxcvbnmqwerty

uiopasdfghjklzxcvbnmqwertyuiopasd
fghjklzxcvbnmqwertyuiopasdfghjklzx
cvbnmqwertyuiopasdfghjklzxcvbnmq
wertyuiopasdfghjklzxcvbnmqwertyui
Programming for
opasdfghjklzxcvbnmqwertyuiopasdfg
Problem Solving
hjklzxcvbnmqwertyuiopasdfghjklzxc
NOTES
vbnmqwertyuiopasdfghjklzxcvbnmq
wertyuiopasdfghjklzxcvbnmqwertyui
UNIT - 2
opasdfghjklzxcvbnmqwertyuiopasdfg
hjklzxcvbnmqwertyuiopasdfghjklzxc
vbnmqwertyuiopasdfghjklzxcvbnmq
Rajesh Tripathi
wertyuiopasdfghjklzxcvbnmqwertyui
SIET

opasdfghjklzxcvbnmqwertyuiopasdfg
hjklzxcvbnmrtyuiopasdfghjklzxcvbn
mqwertyuiopasdfghjklzxcvbnmqwert
yuiopasdfghjklzxcvbnmqwertyuiopas
Sssss
1
Q. What is Operator?Explain types operatorused in C Language?

ANS: Simple answer can be given using expression 4 + 5 is equal to 9. Here 4 and 5 are called operands
and + is called operator. C language supports following type of operators.

 Arithmetic Operators
 Logical Operators
 Relational Operators
 Bitwise Operators
 Assignment Operators
 Misc Operators

Arithmetic Operators:

There are following arithmetic operators supported by C language:

Assume variable A holds 10 and variable B holds 20 then:

Operator Description Example


+ Adds two operands A + B will give 30
- Subtracts second operand from the first A - B will give -10
* Multiply both operands A * B will give 200
/ Divide numerator by denumerator B / A will give 2
Modulus Operator and remainder of after an
% B % A will give 0
integer division
++ Increment operator, increases integer value by one A++ will give 11
-- Decrement operator, decreases integer value by one A-- will give 9

Logical Operators:There are following logical operators supported by C language


Assume variable A holds 10 and variable B holds 20 then:

&& Called Logical AND operator. If both the operands are (A && B) is true.
non zero then then condition becomes true.

Called Logical OR Operator. If any of the two operands


|| (A || B) is true
is non zero then then condition becomes true..
! Called Logical NOT Operator. Use to reverses the logical !(A && B) is false.
state of its operand. If a condition is true then Logical
NOT operator will make false.

Relational operator
Operator Description Example
Checks if the value of two operands is equal or not,
== (A = = B) is not true.
if yes then condition becomes true.

2
Checks if the value of two operands is equal or not,
!= (A != B) is true.
if values are not equal then condition becomes true.
Checks if the value of left operand is greater than
> the value of right operand, if yes then condition (A > B) is not true.
becomes true.
Checks if the value of left operand is less than the
< value of right operand, if yes then condition (A < B) is true.
becomes true.
Checks if the value of left operand is greater than
>= or equal to the value of right operand, if yes then (A >= B) is not true.
condition becomes true.
Checks if the value of left operand is less than or
<= equal to the value of right operand, if yes then (A <= B) is true.
condition becomes true.

Bitwise Operators
Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |, and ^ is as
follows –

p q p&q p|q p^q

0 0 0 0 0

0 1 0 1 1

1 1 1 1 0

1 0 0 1 1

Assume A = 60 and B = 13 in binary format, they will be as follows −


A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
The following table lists the bitwise operators supported by C. Assume variable 'A' holds 60 and variable
'B' holds 13, then −

Operator Description Example

& Binary AND Operator copies a bit to the result if it (A & B) = 12, i.e., 0000 1100
exists in both operands.

3
| Binary OR Operator copies a bit if it exists in either (A | B) = 61, i.e., 0011 1101
operand.

^ Binary XOR Operator copies the bit if it is set in one (A ^ B) = 49, i.e., 0011 0001
operand but not both.

~ Binary One's Complement Operator is unary and has (~A ) = ~(60), i.e,. -0111101
the effect of 'flipping' bits.

<< Binary Left Shift Operator. The left operands value is


moved left by the number of bits specified by the right A << 2 = 240 i.e., 1111 0000
operand.

>> Binary Right Shift Operator. The left operands value is


moved right by the number of bits specified by the right A >> 2 = 15 i.e., 0000 1111
operand.

Assignment operators:
Assignment operators are used for assigning value to a variable. The left side operand of the
assignment operator is a variable and right side operand of the assignment operator is a value. The
value on the right side must be of the same data-type of the variable on the left side otherwise the
compiler will raise an error.
The following table lists the assignment operators supported by the C language –

Operator Description Example

= Simple assignment operator. Assigns values from right side C = A + B will assign the value of A
+= Add AND assignment operator. It adds the right operand to C += A is equivalent to C = C + A
-= Subtract AND assignment operator. It subtracts the right C -= A is equivalent to C = C - A
*= Multiply AND assignment operator. It multiplies the right C *= A is equivalent to C = C * A
/= Divide AND assignment operator. It divides the left operand C /= A is equivalent to C = C / A
%= Modulus AND assignment operator. It takes modulus using C %= A is equivalent to C = C % A
<<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator. C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2
^= Bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C ^ 2
|= Bitwise inclusive OR and assignment operator. C |= 2 is same as C = C | 2

Misc Operators ↦sizeof, &,*, ternary


Besides the operators discussed above, there are a few other important operators including sizeof and ?
: supported by the C Language.
4
Operator Description Example

sizeof() Returns the size of a variable. sizeof(a), where a is integer, will return 4.

& Returns the address of a variable. &a; returns the actual address of the variable.

* Pointer to a variable. *a;

?: If Condition is true ? then value X : otherwise value


Conditional Expression.
Y

Operators Precedence in C
Operator precedence determines the grouping of terms in an expression and decides how an expression is
evaluated. Certain operators have higher precedence than others; for example, the multiplication operator
has a higher precedence than the addition operator.
For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher precedence than
+, so it first gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at
the bottom. Within an expression, higher precedence operators will be evaluated first.

Category Operator Associativity

Postfix () [] -> . ++ - - Left to right


Unary + - ! ~ ++ - - (type)* &sizeof Right to left
Multiplicative */% Left to right
Additive +- Left to right
Shift <<>> Left to right
Relational <<= >>= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= Right to left
^= |=
Comma , Left to right

5
Q. What is conditional control statement? Explain types of statement used in C language.

Selection Statements/conditional Statement


The IF statement, is a powerful decision making statement and is used to control the flow of execution of
statement. It allows the computer to evaluate the expression first and then depending on whether the value
of expression is true or false, it transfers the control to a particular statement.
The following are the selection constructs available in C:
1 simple if statement
2. if-else
3. Nested if
4. if-else if ladder
5. switch statement

simple if statement : it tests the test expression and if the test expression is true , the statement block will
be executed otherwise the statement block will be skipped and the execution will be jumped to the next
statement.
The if statement has three basic forms:

Simple if ()
General Form(Syntax)
if (expression)
{
statements1;
}
Example:
if (a<b)
max = b;
else
max = a;
if else Statement :-Body of an ‘if’ statement contains another ‘if’ statement.
General Form:
if (expression)
statements1;
else
statements2;

6
Example
if (a>b)
printf(“A is greater than B”);
else
printf (“B is greater than A”);
3. Nested if: in nested if statements, another if statement is places either in if branch or in else branch or
in both the branches.
Syntax:
If(expression-1)
If (expression-2)
<statement-1>;
Else
<statement-2>;
Example:
Program to find the maximum of 3 numbers.
if (a>b)
if (a>c)
printf(“largest = %d”, a);
else
printf (“largest = %d”,c);
else
if (c>b)
printf (“largest = %d”,c);
else
printf (“largest = %d”,b);

‘if… else if’ Ladder Statement


General Form:
if (expression)
statements1;
else if (expression)
statements2;
else if(expression)
statements3;
else
statements4;

7
Each condition is evaluated in order and if any condition is true the corresponding statement is executed
and the remainder of the chain is skipped. The final ‘else’ statement is executed only if none of the
previous conditions are satisfied. Final ‘else’ serves as a default case and is useful in detecting an
impossible or error condition.
Example 4.5
if (mark >= 75)
printf(“Honours\n”);
else if (mark >=60)
printf(“First Class\n”);
else if (mark >=50)
printf(“Second Class\n”);
else if (mark >=45)
printf(“Third Class\n”);
else
printf(“Fail\n”);

5) Switch statement in C :

Decision making are needed when, the program encounters the situation to choose a particular statement
among many statements. If a programmer has to choose one among many alternatives if...else can be used
but, this makes programming logic complex. This type of problem can be handled in C programming
using switch...case statement.

In switch...case, expression is either an integer or a character. If the value of switch expression


matches any of the constant in case, the relevant codes are executed and control moves out of the
switch...case statement. If the expression doesn't matches any of the constant in case, then the default
statement is executed.

Syntax :

switch(variable)
{
case 1:
Valid C Statements;
break;
-
-
case n:
Valid C Statements;
break;
default:
Valid C Statements;
break;
}

8
Example

/* C program to demonstrate the working of switch...case statement */


/* Program to create a simple calculator for addition, subtraction, multiplication and division */
# include <stdio.h>
void main(){
charoperator;
float num1,num2;
printf("Enter operator +, - , * or / :\n");
operator=getche();
printf("\nEnter two operands:\n");
scanf("%f%f",&num1,&num2);
switch(operator)
{
case'+':
printf("num1+num2=%.2f",num1+num2);
break;
case'-':
printf("num1-num2=%.2f",num1-num2);
break;
case'*':
printf("num1*num2=%.2f",num1*num2);
break;
case'/':
printf("num2/num1=%.2f",num1/num2);
break;
default:
/* if operator is other than +, -, * or /, error message is shown */
printf(Error!operatorisnot correct");
break;
}
getch();
}

Q what is conditional or ternary operator? Explain.

Conditional / Ternary / ?: Operator


This operator takes 3 expressions / operands. It is a more efficient form for expressing simple if
statements.
General form:
[variable = ]expr1? expr2: expr3;
This simply states:
if (expr1 is true) then expr2 else expr3
Where:
_ expr2 is evaluated, if the value of expr1 is non-zero (true part).
_ expr3 is evaluated, if the value of expr1 is zero (false part).
Example 4.6
max = (a>b) ? a : b;

9
which is similar to the following if-else statement.
if (a>b)
max = a;
else

max = b;

10

You might also like