Operator Precedence

What is Operator Precedence ?
If more than one operators are involved in an expression, it has to be a predefined rule of priority for the operators. This rule of priority of operators is called operator precedence.
EX : 2+4*3 = ?
        6*3=18 or 2+12=14 (which is the right one ???)

BODMAS :
B -  Brackets
O - Orders (i.e. Powers and Square roots)
D - Division
M - Multiplication
A - Addition
S - Subtraction
So with the above rules we can say, 2+4*3 = 2+12 = 14.

What is Associativity ?
If two operators of same precedence (priority) is present in an expression, Associativity of operators indicate the order in which they execute.
  • Left to Right : a + b + c : '+' has left to right associativity. So the expression will be grouped as ((a + b) + c).
  • Right to Left : a = b = c : '=' has right to left associativity. So the expression will be grouped as (a = (b = c)).

Operators with precedence and associativity :
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
Example :
#include <stdio.h>
int 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    : %3.1f\n",  e );
   e = (a + b) * c / d;
   printf("Value of (a + b) * c / d is  : %3.1f\n" ,  e );
   e = a + (b * c) / d;
   printf("Value of a + (b * c) / d is  : %3.1f\n",  e );
   e = a + b * (c / d);    
   printf("Value of a + b * (c / d) is  : %3.1f\n" ,  e );
}

Output:
Value of a + b * c / d is    : 25.0
Value of (a + b) * c / d is  : 22.5
Value of a + (b * c) / d is  : 25.0
Value of a + b * (c / d) is  : 25.0