Order of evaluation of operations can alter the final result. For example, consider evaluating following expression without any mathematics rule.
5 + 3 * 4 / 2
If you do not follow any specific rule to evaluate such expressions, then you probably lead to ambiguous result. Such as one might think it will evaluate either 32 or 11 or 18.5 etc.
To overcome such ambiguity, in mathematics we follow BODMAS rule. Similarly, in computer programming we follow operator precedence and associativity rule.
Precedence of operators
Precedence of an operator specifies its priority compared to other operator. If an expression contain different types of operator, then precedence of operators specifies the order of evaluation each operator.
For example, consider the given expression
int x = 5 + 4 * 2;
The result of above expression is 13 instead of 18. Since
* operator has higher precedence than
+ operator. Hence,
+ operator is evaluated after
Now consider the above expression again with slight change
int x = (5 + 4) * 2;
This time result of above expression is 18. Because precedence of
( ) operator is more than
* operator. Thus
( ) operator evaluates first to 5 + 4 = 9. Then
* operator evaluates to 9 * 2 = 18.
Precedence of an operator affects result of an expression. But, what if two operators have same precedence? This ambiguity is resolved by associativity of operators.
Associativity of operators
If an expression contains more than one operator with same precedence. Then operator precedence along with its associativity defines the order of evaluation of expression.
Operator associativity can either be left-to-right or right-to-left. Means if an expression contains two or more operators of same precedence. Then they are evaluated in either left to right or right to left order.
Consider the below expression
int x = 5 * 4 / 4 % 3;
In the above expression precedence of all three operators are same. Hence, the result of expression is ambiguous (if we follow only precedence). The result can either be 2 or 20 etc. However, order of evaluation ambiguity is resolved if we follow precedence along with associativity rule. All the three operators are left to right associated, hence the expression is guaranteed to evaluate 2.
Consider another operator that associates from right-to-left.
x = y = z;
In the above expression the right-most operator is evaluated first, proceeding towards left. First the value z is assigned to y then the value of y is assigned to x.
Important note: Operators with same precedence have same associativity.
In C programming compilers follow operator precedence and associativity rule to resolve order of evaluation of operators.
Operator precedence and associativity table
|()||Parentheses||1||Left to right|
|||Array element reference|
|->||Member access via pointer|
|.||Member access via object name|
|!||Logical NOT operator||2||Right to left|
|~||Bitwise complement operator|
|+||Unary plus operator|
|-||Unary minus operator|
|sizeof()||Size of operator|
|.*||Dereference operator||3||Left to right|
|*||Multiply||4||Left to right|
|+||Binary addition||5||Left to right|
|<<||Bitwise left shift||6||Left to right|
|>>||Bitwise right shift|
|<||Less than||7||Left to right|
|<=||Less than or equal to|
|>=||Greater than or equal to|
|==||Equal to||8||Left to right|
|!=||Not equal to|
|&||Bitwise AND||9||Left to right|
|^||Bitwise XOR||10||Left to right|
||||Bitwise OR||11||Left to right|
|&&||Logical AND||12||Left to right|
|||||Logical OR||13||Left to right|
|?:||Conditional operator||14||Right to left|
|=||Simple assignment||15||Right to left|
|~=||Assign bitwise complement|
|&=||Assign bitwise AND|
|^=||Assign bitwise XOR|
||=||Assign bitwise OR|
|<<=||Assign bitwise left shift|
|>>=||Assign bitwise right shift|
|,||Comma operator||16||Left to right|