0% found this document useful (0 votes)
8 views8 pages

Operators Precedence

The document provides a comprehensive overview of operator precedence and associativity in programming, detailing various operators and their meanings. It includes examples of code snippets demonstrating the evaluation of expressions based on the defined precedence rules. Additionally, it presents answers to specific programming problems related to operator usage and evaluation outcomes.

Uploaded by

saarthak22305
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

Operators Precedence

The document provides a comprehensive overview of operator precedence and associativity in programming, detailing various operators and their meanings. It includes examples of code snippets demonstrating the evaluation of expressions based on the defined precedence rules. Additionally, it presents answers to specific programming problems related to operator usage and evaluation outcomes.

Uploaded by

saarthak22305
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Operator precedence and Associativity

Summary of operators with precedence and Associativity

Operator Meaning of operator Associativity

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

+ Binary plus(Addition) Left to


- Binary minus(subtraction) right

<< Left shift Left to


>> Right shift right

< Less than


<= Less than or equal
> Greater than Left to
>= Greater than or equal right

== Equal to Left to
!= Not equal to right
Summary of operators with precedence and Associativity

Operator Meaning of operator 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)

a. Z=X+Y*Z/4%2-1 Answer: z=9


b. (A>B)&&(A<B)+(B<=C)*D+E; Answer: 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()

int a = 10, b = 20, c = 30;

// (c > b > a) is treated as ((c > b) > a), associativity of '>'

// is left to right. Therefore the value becomes ((30 > 20) > 10)

// which becomes (1 > 20)

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

. What is the output of this C code?


1. #include <stdio.h>
2. int main()
3. {
4. int x = 1, y = 0, z = 3;
5. x > y ? printf("%d", z) : return z;
6. }
Answer: z=3

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

What is the final value of j in the below code?


1. #include <stdio.h>
2. int main()
3. {
4. int i = 0, j = 0;
5. if (i && (j = i + 10))
6. //do something
7. ;
8. }

Answer: j=0;

What is the output of this C code?


1. #include <stdio.h>
2. int main()
3. {
4. int i = 1;
5. if (i++ && (i == 1))
6. printf("Yes\n");
7. else
8. printf("No\n");
9. }

Answer: NO

Result of a logical or relational expression in C is


a) True or False
b) 0 or 1
c) 0 if expression is false and any positive number if expression is true
d) None of the mentioned

Answer: 0 or 1

What will be the value of d in the following program?


1. #include <stdio.h>
2. int main()
3. {
4. int a = 10, b = 5, c = 5;
5. int d;
6. d = b + c == a;
7. printf("%d", d);
8. }

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

Answer: a=2 b=1

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

What is the output of this C code?


1. #include <stdio.h>
2. void main()
3. {
4. int k = 8;
5. int m = 7;
6. int z = k < m ? k++ : m++;
7. printf("%d", z);
8. }
1. }

Answer: z=7

You might also like