Operators Precedence
Operators Precedence
() Functional call
[] Array element reference
-> Indirect member selection Left to
. Direct member selection right
! Logical negation
~ Bitwise(1 's) complement
+ Unary plus
- Unary minus
++ Increment
-- Decrement
& Dereference
* Operator(Address)
sizeo Pointer reference
f Returns the size of an
(type object Right to
) Type cast(conversion) left
* Multiply
/ Divide Left to
% Remainder right
== Equal to Left to
!= Not equal to right
Summary of operators with precedence and Associativity
Left to
& Bitwise AND right
Left to
^ Bitwise exclusive OR right
Left to
| Bitwise OR right
Left to
&& Logical AND right
Left to
|| Logical OR right
Left to
?: Conditional Operator right
Simple assignment
= Assign product
*= Assign quotient
/= Assign remainder
%= Assign sum
-= Assign difference
&= Assign bitwise AND
^= Assign bitwise XOR
|= Assign bitwise OR
<<= Assign left shift Right to
>>= Assign right shift left
Left to
, Separator of expressions right
#include<stdio.h>
int main()
{ int a;
a = 1, 2, 3; // Evaluated as (a = 1), 2, 3
printf("%d", a);
return 0;
Answer: a=1
Solve:
i) a= 4/2*(2-6)-7^2 Answer: a= -13
ii) c=(2==2)?(5++):(- -6) Answer: Error (5++ or –6 can not be
evaluated)
void main()
{
int x=2,y=0,r;
int z;
z=y=1;
r=x&&y;
printf(“%d”,z);
}
Answer: z=1
Evaluate the expression & represent the order of evaluation based on precedence
with neat diagram. (X=10,Y=2,Z=4,A=12,B=5,C=13,D=9,E=1)
c. void main()
{
int x=2,y=0;
int z= (y++)?2:y==1&&x;
printf(“z=%d”,z);
}
Answer: z=1
#include <stdio.h>
int main()
// is left to right. Therefore the value becomes ((30 > 20) > 10)
if (c > b > a)
printf("TRUE");
else
printf("FALSE");
return 0;
#include <stdio.h>
int main()
int a=10,b=20,c=30,d;
d=a>b?a:c>a?c:a;
printf("%d",d);
}
Answer: d=30
1. #include <stdio.h>
2. void main()
3. {
4. int x = 1, y = 0, z = 5;
5. int a = x && y || z++;
6. printf("%d", z);
7. }
Answer: z=6
1. #include <stdio.h>
2. void main()
3. {
4. int x = 0, y = 2, z = 3;
5. int a = x & y | z;
6. printf("%d", a);
7. }
Answer: a=3
Answer: j=0;
Answer: NO
Answer: 0 or 1
Answer: d=1
1. #include <stdio.h>
2. int main()
3. {
4. int a = 1, b = 1, c;
5. c = a++ + b;
6. printf("%d, %d", a, b);
7. }
1. #include <stdio.h>
2. int main()
3. {
4. int a = 10, b = 10;
5. if (a = 5)
6. b--;
7. printf("%d, %d", a, b--);
8. }
Answer: a=5 b=9
1. #include <stdio.h>
2. int main()
3. {
4. int i = 2;
5. int j = ++i + i;
6. printf("%d\n", j);
7. }
Answer: j=6
What is the output of this C code?
1. #include <stdio.h>
2. int main()
3. {
4. int x = 1;
5. short int i = 2;
6. float f = 3;
7. if (sizeof((x == 2) ? f : i) == sizeof(float))
8. printf("float\n");
9. else if (sizeof((x == 2) ? f : i) == sizeof(short int))
10. printf("short int\n");
11. }
Answer: Short int
Answer: z=7