Operator Precedence and Associativity in C Programming

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.

Operator Associativity in C Language

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.

Operator Precedence and Associativity Table in C Programming

OperatorDescriptionPrecedenceAssociativity
( )Parentheses1Left to right
[ ]Array element reference
->Member access via pointer
.Member access via object name
++Postfix increment
Postfix decrement
!Logical NOT operator2Right to left
~Bitwise complement operator
+Unary plus operator
Unary minus operator
++Prefix Increment
Prefix Decrement
*Indirection operator
sizeof()Size of operator
(type)Type cast
.*Dereference operator3Left to right
->*Dereference operator
*Multiply4Left to right
/Divide
%Modulus
+Binary addition5Left to right
Binary subtraction
<<Bitwise left shift6Left to right
>>Bitwise right shift
<Less than7Left to right
<=Less than or equal to
>Greater than
>=Greater than or equal to
==Equal to8Left to right
!=Not equal to
&Bitwise AND9Left to right
^Bitwise XOR10Left to right
|Bitwise OR11Left to right
&&Logical AND12Left to right
||Logical OR13Left to right
?:Conditional operator14Right to left
=Simple assignment15Right to left
*=Assign product
~=Assign bitwise complement
/=Assign quotient
%=Assign remainder
+=Assign sum
-=Assign difference
&=Assign bitwise AND
^=Assign bitwise XOR
|=Assign bitwise OR
<<=Assign bitwise left shift
>>=Assign bitwise right shift
,Comma operator16Left to right
Operator Precedence and Associativity in C Programming

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 praveen171086@gmail.com.

You can also visit my Hindi YouTube Channel.

Leave a Comment