Table below shows the precedence and associativity of the operators available in C language. The operators in top rows have more precedence compared to the ones in bottom rows. Operators in same cell have same precedence.
Operator(s) | Description | Associativity |
---|---|---|
++ -- | Post increment/post decrement operator | Left to right associativity |
++ -- | Pre-increment/pre-decrement operator | Right to left associativity |
+ - | Unary plus/minus operator | Right to left associativity |
! ~ | Logical NOT/bitwise NOT operator | Right to left associativity |
* | "Value at address" operator | Right to left associativity |
& | "Address of" operator | Right to left associativity |
sizeof | "Size of" operator | Right to left associativity |
* / % | Multiplication/division/modulus operator | Left to right associativity |
+ - | Addition/subtraction operator | Left to right associativity |
<< >> | Bitwise left shift/bitwise right shift operator | Left to right associativity |
< <= | "Less than"/"Less than or equal to" relational operator | Left to right associativity |
> >= | "Greater than"/"Greater than or equal to" relational operator | Left to right associativity |
== != | "Equal to"/"Not equal to" relational operator | Left to right associativity |
& | Bitwise AND operator | Left to right associativity |
^ | Bitwise XOR operator | Left to right associativity |
| | Bitwise OR operator | Left to right associativity |
&& | Logical AND operator | Left to right associativity |
|| | Logical OR operator | Left to right associativity |
?: | Ternary conditional operator | Right to left associativity |
= | Assignment operator | Right to left associativity |
+= -= | Assignment with addition/subtraction | Right to left associativity |
*= /= %/ | Assignment with multiplication/division/modulus | Right to left associativity |
<<= >>= | Assignment with bitwise left shift/bitwise right shift | Right to left associativity |
&= ^= |= | Assignment with bitwise AND/bitwise XOR/bitwise OR | Right to left associativity |
precedence - Associativity Examples
- #include <stdio.h>
- main()
- {
- float a = 10;
- float b = 20;
- float c = 30;
- float d = 40;
- float e;
- e = a + b * c / d;
- printf("Value of a + b * c / d is : %f\n", e );
- e = (a + b) * c / d;
- printf("Value of (a + b) * c / d is : %f\n" , e );
- e = a + (b * c) / d;
- printf("Value of a + (b * c) / d is : %f\n", e );
- e = a + b * (c / d);
- printf("Value of a + b * (c / d) is : %f\n" , e );
- return 0;
- }
- Output:
- Value of a + b * c / d is : 25.000000
- Value of (a + b) * c / d is : 22.500000
- Value of a + (b * c) / d is : 25.000000
- Value of a + b * (c / d) is : 25.000000
1 comment:
very useful information. thanks for sharing.
Get all recruitment details like bank recruitment.
Post a Comment