# 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.

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.

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 pr***********@gm***.com.