In this post, we will talk about operator precedence and association in C Programming and also see some examples related to it so that you can understand this topic better.
You all must have read the BODMAS rule in math, using which we decide the operations order while solving the expression (e.g. 12 + 5 x 13 / 2 – 1).
Similarly, in C programming also, while evaluating the expression, the rules of operator precedence and association have to be followed.
You can also read: Operators In C Programming
When more than one operator is used in an expression, then all operators are given priority by operator precedence.
It is decided on the basis of the operator’s priorities that which operator will be solved first while evaluating the expression. Let us understand with example.
2 + 5 * 3 = 7 * 3 = 21 (wrong) 2 + 5 * 3 = 2 + 15 = 17 (right)
If we solve the expression without following the rules of operator precedence, then our output will also be wrong like the first example given above.
Because in C language the operator precedence of multiply ( * ) is more than the operator precedence of addition ( + ).
Therefore, first 5 * 3 will be evaluated, after that 2 will be added to it, then you will get the correct output like in the second example. Let us understand another example.
(2 + 5) * 3 = 7 * 3 = 21
In the above expression, we put 2 + 5 inside parentheses ( ) and the operator precedence of parentheses is more than the * operator.
Therefore, first 2 + 5 will be evaluated, after that 3 will be multiplied, then you will get the correct output.
When more than one operator with the same precedence is used in an expression, then the order of evaluating those operators (left to right or right to left) is decided by operator association. Let us understand with example.
5 * 3 + 8 / 2
The operator precedence (priority) of * and / is the same in the above expression, so now here we will solve this expression by looking at the associativity of these operators.
In the table given below, a list of C operator precedence and association is given, which you should see carefully and remember.
|( )||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|
I hope that you have liked this post. If you liked the post, then please share it with your friends and relatives and you can send your suggestion in the comment box below or email me at email@example.com.