Avinash Singh Programming,C Programming Operator Precedence and Associativity in C Programming

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

## The best IDEs to learn JavaThe best IDEs to learn Java

YouÂ wantÂ toÂ getÂ startedÂ programming Java, but youÂ donâ€™tÂ wantÂ theÂ toolsÂ toÂ getÂ in theÂ way?Â TryÂ theseÂ clean,Â simple, andÂ friendlyÂ Java IDEs. You say you want to learn Java. It can be a daunting language. It might indeed feel impenetrable if you â€™re a new

## What is C language and how to learn?What is C language and how to learn?

There are many questions in the mind of many about what is C Language and how to learn C Language. If you want to become a good Software Engineer, then

## A Beginner’s Guide to Creating GUI Applications in C Programming LanguageA Beginner’s Guide to Creating GUI Applications in C Programming Language

GUI (Graphical User Interface) applications using C programming refer to programs that have a graphical interface designed to interact with the user visually, using windows, buttons, menus, and other graphical