Thursday, February 4, 2016

Precedence and Associativity Table

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)DescriptionAssociativity
++ --Post increment/post decrement operatorLeft to right associativity
++ --Pre-increment/pre-decrement operatorRight to left associativity
+ -Unary plus/minus operatorRight to left associativity
! ~Logical NOT/bitwise NOT operatorRight to left associativity
*"Value at address" operatorRight to left associativity
&"Address of" operatorRight to left associativity
sizeof"Size of" operatorRight to left associativity
* / %Multiplication/division/modulus operatorLeft to right associativity
+ -Addition/subtraction operatorLeft to right associativity
<< >>Bitwise left shift/bitwise right shift operatorLeft to right associativity
< <="Less than"/"Less than or equal to" relational operatorLeft to right associativity
> >="Greater than"/"Greater than or equal to" relational operatorLeft to right associativity
== !="Equal to"/"Not equal to" relational operatorLeft to right associativity
&Bitwise AND operatorLeft to right associativity
^Bitwise XOR operatorLeft to right associativity
|Bitwise OR operatorLeft to right associativity
&&Logical AND operatorLeft to right associativity
||Logical OR operatorLeft to right associativity
?:Ternary conditional operatorRight to left associativity
=Assignment operatorRight to left associativity
+= -=Assignment with addition/subtractionRight to left associativity
*= /= %/Assignment with multiplication/division/modulusRight to left associativity
<<= >>=Assignment with bitwise left shift/bitwise right shiftRight to left associativity
&= ^= |=Assignment with bitwise AND/bitwise XOR/bitwise ORRight to left associativity

precedence - Associativity Examples

  1. #include <stdio.h>
  2.  
  3. main()
  4. {
  5. float a = 10;
  6. float b = 20;
  7. float c = 30;
  8. float d = 40;
  9. float e;
  10. e = a + b * c / d;
  11. printf("Value of a + b * c / d is : %f\n", e );
  12.  
  13. e = (a + b) * c / d;
  14. printf("Value of (a + b) * c / d is : %f\n" , e );
  15.  
  16. e = a + (b * c) / d;
  17. printf("Value of a + (b * c) / d is : %f\n", e );
  18.  
  19. e = a + b * (c / d);
  20. printf("Value of a + b * (c / d) is : %f\n" , e );
  21. return 0;
  22. }
  23.  
  24. Output:
  25.  
  26. Value of a + b * c / d is : 25.000000
  27. Value of (a + b) * c / d is : 22.500000
  28. Value of a + (b * c) / d is : 25.000000
  29. Value of a + b * (c / d) is : 25.000000

1 comment:

Anonymous said...

very useful information. thanks for sharing.

Get all recruitment details like bank recruitment.