02 Operators Updated
02 Operators Updated
By:Dr. P.S.Tanwar
Operators
Operators
According to number of operands there are
three types of operators
• Unary Operator: 1 operand
– -x,-y,++x,--x, !,~ etc.
By:Dr. P.S.Tanwar
Operators
According to Operations
1. Arithmetic Operator
2. Relational Operator
3. Equality Operator
4. Bitwise Operator
5. Logical Operator
7. Assignment Operator
9. Special Operator
By:Dr. P.S.Tanwar
Arithmetic Operator
If x=10 and y=6
By:Dr. P.S.Tanwar
Arithmetic Operator
Ex Ex
int / int=int (i)13/ 5
int/float=float = 2
(ii)13.0/ 5
float/float=float
= 2.6
float/int=float
(ii)13/ 5.0
= 2.6
(iv)13.0/ 5.0
= 2.6
By:Dr. P.S.Tanwar
Arithmetic Operator
Ex
3%2 = 1
-3 % 2 = -1
3 % -2 = 1
-3 % -2 = -1
By:Dr. P.S.Tanwar
Arithmetic Operator
Precedence
Internal
U ARE BLCAC
• I: *, / , %
• II: +, -
With Others
• Unary >Arithmetic> Relational>Equality> Bitwise>
Logical>Conditional> Assignment> Comma
By:Dr. P.S.Tanwar
Arithmetic Operator
Precedence With Others
• Unary ( -,+,++,--,~,!, sizeof, etc. ) >
• Comma ( , )
By:Dr. P.S.Tanwar
Arithmetic Operator
Associativity
Used when we have more than two operators
having same precedence.
Left to Right
2+3*5/6
= 2 + 15/ 6
= 2+2
= 4
By:Dr. P.S.Tanwar
Arithmetic Operator
Ex
2 + 3 * (5 / 6)
= 2 + 3*0
= 2+0
=2
By:Dr. P.S.Tanwar
Relational Operator
a =10,b=6
By:Dr. P.S.Tanwar
Relational Operator
Precedence With Others
Unary ( -,+,++,--,~,!, sizeof, etc. ) >
Comma ( , )
int x=2;
printf(“%d\n”,x>5);
printf(“%d\n”,x<5);
By:Dr. P.S.Tanwar
Equality Operator
By:Dr. P.S.Tanwar
Equality Operator
Precedence With Others
• Unary ( -,+,++,--,~,!, sizeof, etc. ) >
• Comma ( , )
By:Dr. P.S.Tanwar
Logical Operator
&&
And operator
By:Dr. P.S.Tanwar
Logical Operator
||
Or operator
C1 C2 C1 || C2
0 0 0
0 1 1
1 0 1
1 1 1
By:Dr. P.S.Tanwar
Logical Operator
!
Not operator
• ii) &&
• iii) ||
By:Dr. P.S.Tanwar
Logical Operator
Precedence With Others
• Unary ( -, +, ++, --, ~, ! , sizeof, etc. ) >
• Comma ( , )
I Bitwise or 12 I 10 14
^ Exclusive or 4 ^7 3
~ Complement ~ 12 -13
<< Left shift x=12<<1 x=24
By:Dr. P.S.Tanwar
Bitwise Operator
Bitwise and (&) Output
84 21 8
1100 short x=12,y=10;
8+4=12
printf(“%d”,x&y);
128 64 32 16 8 4 2 1
0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 x
8 4 2 1
& 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 y
8 4 2 1
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 =(8)10
Note: If compiler is using 2 bytes for short then above memory allocation will be there.
By:Dr. P.S.Tanwar
Bitwise Operator
Bitwise or (|) Output
14
short x=12,y=10;
printf(“%d”,x|y);
0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 x
| 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 y
8 4 2 1
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 =(14)10
Note: If compiler is using 2 bytes for short then above memory allocation will be there.
By:Dr. P.S.Tanwar
Bitwise Operator
Bitwise xor (^) Output
6
short x=12,y=10;
printf(“%d”,x^y);
0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 x
^ 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 y
8 4 2 1
0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 =(6)10
Note: If compiler is using 2 bytes for short then above memory allocation will be there.
By:Dr. P.S.Tanwar
Bitwise Operator
Complement Operator(~) Output
-13
short x=12;
printf(“%d”,~x);
x 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0
~x 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1
Note: If compiler is using 2 bytes for short then above memory allocation will be there.
By:Dr. P.S.Tanwar
Bitwise Operator
Left shift (x<<n)
Shift n bits of x in the left side.
Output
• short x=12; 24
• printf(“%d”,x<<1);
8 4 2 1
0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 x
16 8 4 2 1
0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 =(24)10
Note: If compiler is using 2 bytes for short then above memory allocation will be there.
By:Dr. P.S.Tanwar
Bitwise Operator
Right shift (x>>n)
Shift n bits of x in the right side.
Output
• short x=12; 6
• printf(“%d”,x>>1);
8 4 2 1
0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 x
16 8 4 2 1
0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 =(6)10
Note: If compiler is using 2 bytes for short then above memory allocation will be there.
By:Dr. P.S.Tanwar
Bitwise Operator
Precedence With Others
Internal Precedence
• i) ~
• ii) <<,>>
• iii) &
• iv) ^
• v) I
By:Dr. P.S.Tanwar
Bitwise Operator
Precedence With Others
• Unary ( -, + , ++, -- , ~ , ! , sizeof, etc. ) >
• Comma ( , )
By:Dr. P.S.Tanwar
Conditional Operator
Ex
x Output
main
5 y=2
{ 2 y
int x=5;
false 2
(0)
printf(“y=%d\n”,y);
By:Dr. P.S.Tanwar
Conditional Operator
Precedence With Others
• Unary ( -,+,++,--,~,!, sizeof, etc. ) >
• Comma ( , )
By:Dr. P.S.Tanwar
Assignment Operator
Precedence With Others
• Unary ( -,+,++,--,~,!, sizeof, etc. ) >
• Comma ( , )
Pre Increment
/ Pre
Increment / Decrement
Decrement Post Increment
/ Post
Decrement
By:Dr. P.S.Tanwar
Increment and Decrement
Operator
Pre Increment Operator
1st : increment
2nd : assign
By:Dr. P.S.Tanwar
Increment and Decrement
Operator
Pre Increment Operator 1st : increment
2nd : assign
a) ++x; x=x+1;
x Output
int x=5;
5 x=6
++x; 6
printf(“x=6\n”); printf(“x=%d\n”,x);
By:Dr. P.S.Tanwar
Increment and Decrement
Operator
Pre Increment Operator
x=x+1;
1st : increment
(b) y=++x;
y=x; 2nd : assign
x y
5 6
int x=5,y; Output
6 x=6
y=++x; y=6
printf(“x=%d\ny=%d”,x,y);
x Output
int x=5;
5 x=6
x++; 6
printf(“x=6\n”); printf(“x=%d\n”,x);
By:Dr. P.S.Tanwar
Increment and Decrement
Operator
Post Increment Operator
y=x; 1st : assign
(b) y=x++;
x=x+1; 2nd : increment
x y
5 5
int x=5,y; Output
5
6 x=6
y=x++; y=5
printf(“x=%d\ny=%d”,x,y);
printf(“x=%d\n”,x);
x Output
int x=5;
6 5 x=6
printf(“x=%d\n”,++x); 6 x=6
printf(“x=%d\n”,x);
By:Dr. P.S.Tanwar
Increment and Decrement
Increment Operator x
Output 5
x=5
x=7
x=6
66
X=5 7
8
int x=5;
7 6 5
printf(“x=%d\n x=%d\n x=%d\n”,x++,x++,x++);
By:Dr. P.S.Tanwar
Increment and Decrement
Operator
Pre Decrement Operator 1st : decrement
2nd : assign
a) --x; x=x-1;
x Output
int x=5;
5 x=4
--x; 4
printf(“x=4\n”); printf(“x=%d\n”,x);
By:Dr. P.S.Tanwar
Increment and Decrement
Operator
Pre Decrement Operator
x=x-1; 1st : Decrement
(b) y=--x; y=x; 2nd : assign
x y
5 4
int x=5,y; Output
4
4 x=4
y=--x; y=4
printf(“x=%d\ny=%d”,x,y);
x Output
int x=5;
5 x=4
x--; 4
printf(“x=4\n”); printf(“x=%d\n”,x);
By:Dr. P.S.Tanwar
Increment and Decrement
Operator
Post Decrement Operator
y=x; 1st : assign
(b) y=x--;
x=x-1; 2nd : Decrement
x y
5 5
int x=5,y; Output
5
4 x=4
y=x-- ; y=5
printf(“x=%d\ny=%d”,x,y);
• Comma ( , )
(ii) &
(iii) *
(iv) sizeof
(v) (type)
By:Dr. P.S.Tanwar
Special Operator
Comma Operator
, is used to separate
• Ex int x,y,z;
& : address
By:Dr. P.S.Tanwar
Special Operator
* : pointer
sizeof:
size of var. or datatype
• Ex: sizeof(int)
• Ans. 2
(type):
int x=(int)4.5;
By:Dr. P.S.Tanwar
Thank You
By
By:Dr. P.S.Tanwar