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