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

## You can also visit my Hindi YouTube Channel. I am Avinash Singh, a software engineer from the profession and have been working in the IT industry since 2009. I live in Barabanki district of Uttar Pradesh in India and completed intermediate in 2001 from Ram Sevak Inter College, a UP Board school. After inter I completed my professional degree (BCA & MCA) from Indira Gandhi National Open University, Delhi in 2008 and along with it I did many certifications to keep my knowledge up to date and the next year I started a software development company E – Vision Technocraft Pvt Ltd.

### Related Post ## Why we learn programming Languages?Why we learn programming Languages?

Suppose that there are two person A and B. Both of them know two different languages X and Y respectively. Now the question is that if they could communicate with each ## Operators in C ProgrammingOperators in C Programming

In this post we will talk about Operators in C Programming but before that we will understand Expressions and Operands because both these terms will be used very often 