0% found this document useful (0 votes)
17 views55 pages

02 Operators Updated

aboyt c prohrammingAC

Uploaded by

ankush02481
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)
17 views55 pages

02 Operators Updated

aboyt c prohrammingAC

Uploaded by

ankush02481
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/ 55

Operators

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.

• Binary Operator: 2 operand


– x+y,x-y, etc

• Ternary Operator: 3 operand


– Cond ? valueIfTrue: valueIfFalse

By:Dr. P.S.Tanwar
Operators
According to Operations
1. Arithmetic Operator

2. Relational Operator

3. Equality Operator

4. Bitwise Operator

5. Logical Operator

By:Dr. P.S.Tanwar to be continue…


Operators
According to Operations
6. Conditional Operator

7. Assignment Operator

8. Increment Decrement Operator

9. Special Operator

By:Dr. P.S.Tanwar
Arithmetic Operator
If x=10 and y=6

Operator Meaning Expression Value


+ Addition x+y 16
- Subtraction x-y 4
* Multiplication x*y 60
/ Division x/y 1
% Mod x%y 4
(Remainder)

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. ) >

• Arithmetic(1. *,/,%, 2.+,-) > Bitwise(<<,>>)>

• Relational (<,<=,>,>=) > Equality (==,!= ) >

• Bitwise(1. &, 2.^, 3. I) Logical (1. &&, 2. II ) >

• Conditional ( ? :) > Assignment(=,+=,-=,*=,/= etc.)>

• Comma ( , )

By:Dr. P.S.Tanwar
Arithmetic Operator
Associativity
Used when we have more than two operators
having same precedence.

Left to Right

Note: Only unary, ternary(?:) and


assignment operators have Right to Left
Associativity
By:Dr. P.S.Tanwar
Arithmetic Operator
Ex
x Output
int x=2 + 3 * 5 / 6;
4 x=4
printf(“x=%d”,x);

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

Operator Meaning expression value

< Less than a<b 0(false)

<= Less than equal to a<=b 0(false)

> Greater than a>b 1(true)

>= Greater than equal a>=b 1(true)


to

By:Dr. P.S.Tanwar
Relational Operator
Precedence With Others
Unary ( -,+,++,--,~,!, sizeof, etc. ) >

Arithmetic(1. *,/,%, 2.+,-) > Bitwise(<<,>>)>

Relational (<,<=,>,>=) > Equality (==,!= ) >

Bitwise(1. &, 2.^, 3. I) Logical (1. &&, 2. II ) >

Conditional ( ? :) > Assignment(=,+=,-=,*=,/= etc.)

Comma ( , )

Associativity : Left to Right


By:Dr. P.S.Tanwar
Relational Operator
Ex
x Output
main
2 0
{ 1

int x=2;

printf(“%d\n”,x>5);

printf(“%d\n”,x<5);

By:Dr. P.S.Tanwar
Equality Operator

Operator Meaning Expression Value

== Equal to (compare) 10==20 0(false)

!= Not equal to 10!=20 1(true)

By:Dr. P.S.Tanwar
Equality Operator
Precedence With Others
• Unary ( -,+,++,--,~,!, sizeof, etc. ) >

• Arithmetic(1. *,/,%, 2.+,-) > Bitwise(<<,>>)>

• Relational (<,<=,>,>=) > Equality (==,!= ) >

• Bitwise(1. &, 2.^, 3. I) Logical (1. &&, 2. II ) >

• Conditional ( ? :) > Assignment(=,+=,-=,*=,/= etc.)

• Comma ( , )

Associativity : Left to Right


By:Dr. P.S.Tanwar
Logical Operator
x=10, y=20, z=30
Operator Meaning Expression value

&& Logical and x > y && y < z 0


0 && 1

II Logical or x > y II y < z 1


0 || 1

! Logical not ! (x > y) 1


!0

By:Dr. P.S.Tanwar
Logical Operator
&&
And operator

If both conditions are true(1) then the result will


be true otherwise the result will be false(0)
C1 C2 C1 && C2
0 0 0
0 1 0
1 0 0
1 1 1

By:Dr. P.S.Tanwar
Logical Operator
||
Or operator

If any of the condition is true(1) then the result will be true


otherwise the result will be false(0)

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

If the given conditions is true(1) then the result will be


false and If the given conditions is false(0) then the result
will be true
Cond1 !Cond1
0 1
1 0
By:Dr. P.S.Tanwar
Logical Operator
Precedence
Internal Precedence
• i) !

• ii) &&

• iii) ||

By:Dr. P.S.Tanwar
Logical Operator
Precedence With Others
• Unary ( -, +, ++, --, ~, ! , sizeof, etc. ) >

• Arithmetic(1. *,/,%, 2.+,-) > Bitwise(<<,>>)>

• Relational (<,<=,>,>=) > Equality (==,!= ) >

• Bitwise(1. &, 2.^, 3. I) Logical (1. &&, 2. II ) >

• Conditional ( ? :) > Assignment(=,+=,-=,*=,/= etc.)

• Comma ( , )

Associativity : Left to Right


By:Dr. P.S.Tanwar
Bitwise Operator
Operator Meaning Expression Value

& Bitwise and 12&10 8

I Bitwise or 12 I 10 14

^ Exclusive or 4 ^7 3

~ Complement ~ 12 -13
<< Left shift x=12<<1 x=24

>> Right shift y=12>>1 y=6

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. ) >

• Arithmetic(1. *,/,%, 2.+,-) > Bitwise(<<,>>)>

• Relational (<,<=,>,>=) > Equality (==,!= ) >

• Bitwise(1. &, 2.^, 3. I) Logical (1. &&, 2. II ) >

• Conditional ( ? :) > Assignment(=,+=,-=,*=,/= etc.)

• Comma ( , )

Associativity : Left to Right


By:Dr. P.S.Tanwar
Conditional Operator
?:
Condition ? Expression1: Expression2

If the condition is true then it will


execute Expression1 otherwise it will
execute Expression2

By:Dr. P.S.Tanwar
Conditional Operator
Ex
x Output
main
5 y=2
{ 2 y
int x=5;
false 2
(0)

int y = x>10 ? 1 : 2; int y=2;

printf(“y=%d\n”,y);

By:Dr. P.S.Tanwar
Conditional Operator
Precedence With Others
• Unary ( -,+,++,--,~,!, sizeof, etc. ) >

• Arithmetic(1. *,/,%, 2.+,-) > Bitwise(<<,>>)>

• Relational (<,<=,>,>=) > Equality (==,!= ) >

• Bitwise(1. &, 2.^, 3. I) Logical (1. &&, 2. II ) >

• Conditional ( ? :) > Assignment(=,+=,-=,*=,/= etc.)

• Comma ( , )

Associativity : Right to Left


By:Dr. P.S.Tanwar
Assignment Operator
x=5
Operator Expression Meaning Value

= x=10 Equal (assignment) 10


+= x+=2 x=x+2 7
-= x-=2 x=x-2 3
*= x*=2 x=x*2 10
/= x/=2 x=x/2 2
%= x%=2 x=x%2 1
&= x&=2 x=x&2 0
|= x|=2 x=x|2 7
^= x^=2 x=x^2 7

By:Dr. P.S.Tanwar
Assignment Operator
Precedence With Others
• Unary ( -,+,++,--,~,!, sizeof, etc. ) >

• Arithmetic(1. *,/,%, 2.+,-) > Bitwise(<<,>>)>

• Relational (<,<=,>,>=) > Equality (==,!= ) >

• Bitwise(1. &, 2.^, 3. I) Logical (1. &&, 2. II ) >

• Conditional ( ? :) > Assignment(=,+=,-=,*=,/= etc.)

• Comma ( , )

Associativity : Right to Left


By:Dr. P.S.Tanwar
Increment and Decrement
Operator

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);

printf(“x=6\ny=6”); By:Dr. P.S.Tanwar


Increment and Decrement
Operator
Post Increment Operator 1st : assign
2nd : increment
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
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=6\ny=5”); By:Dr. P.S.Tanwar


Increment and Decrement
Operator
Post Increment Operator
(b) x y 1st : assign
5 5 2nd : increment
int x=5,y;
5
6
y=x>2? x++: 10 ;
Rough Work Output
y=x>2? 5 : 10 ; printf(“x=%d\ny=%d”,x,y); x=6
y=1 ? 5 : 10 ; y=5
y=5;
printf(“x=6\ny=5”);
By:Dr. P.S.Tanwar
Increment and Decrement
Increment Operator
x Output
int x=5;
5 5 x=5
printf(“x=%d\n”,x++); 6 x=6

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);

printf(“x=4\ny=4”); By:Dr. P.S.Tanwar


Increment and Decrement
Operator
Post Decrement Operator 1st : assign
2nd : Decrement
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
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);

printf(“x=4\ny=5”); By:Dr. P.S.Tanwar


Increment /Decrement
Operator
Precedence With Others
• Unary ( -,+,++,--,~,!, sizeof, etc. ) >

• Arithmetic(1. *,/,%, 2.+,-) > Bitwise(<<,>>)>

• Relational (<,<=,>,>=) > Equality (==,!= ) >

• Bitwise(1. &, 2.^, 3. I) Logical (1. &&, 2. II ) >

• Conditional ( ? :) > Assignment(=,+=,-=,*=,/= etc.)

• Comma ( , )

Associativity : Right to Left


By:Dr. P.S.Tanwar
Special Operator
(i) ,

(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

Ex : &x means address of x

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

Dr. Prakash Singh Tanwar

By:Dr. P.S.Tanwar

You might also like