In C programming, the operator precedence determines the order or the priority of the operators when multiple operators are in their expression. We call these operators symbols that tell the compiler to perform specific mathematical, logical, or relational operations. When an expression contains multiple operators, the compiler must decide the order in which to apply them, ultimately maintaining the ambiguity of the expression and avoiding the use of parenthesis. The decision to solve the expression is governed by two key rules: operator precedence and associativity.
In this article, we will go through the operator precedence and associativity in C through which we decide the priority of operators. We’ll see the associativity order of lefttoright and righttoleft. Along with this, we will explore the indepth explanations, examples, and the operator precedence and associativity table for more information. Let’s get started.
Operator precedence refers to the rules in C that determine the order in which the different operators are evaluated in an expression. The rule of operator precedence states that operators possessing higher priority levels should be evaluated ahead of those with lower priorities.
Understanding the operator precedence is crucial in C programming, as it plays a significant role in ensuring the proper evaluation of an expression. When dealing with multiple operators within an expression, this rule guides us in identifying the primary operator that needs to be evaluated first.
Let’s understand the operator precedence of an expression in C language with the help of an example. We will now evaluate the following expression using the operator precedence:
Expression:
In this example, we have an expression that involves multiplication and addition operations. As per the operator precedence table, the multiplication operator (*) will have a higher precedence than (+) addition. Therefore, as the precedence, 4 * 5 will be evaluated first, resulting in 20, and then 10 + 20 is evaluated to get the final answer as 30.
But now, if you use the parenthesis, the order of precedence and the expression answer will be changed. Let’s see how.
Here, as per the operator precedence table, the parenthesis operator () will have higher precedence than the multiplication (*) operator and, finally, the (+) addition operator. Therefore, the precedence of 10 + 4 will be evaluated first to give the result of 14. Then 14 * 5 is evaluated to get the final result as 70.
We have now learned the concept of how operator precedence ensures that the multiplication operation takes precedence over addition. Let’s now see the expression evaluation using operator precedence in C.
Code Example 1: Using operator precedence to solve an expression in C.
Output:
Explanation:
In this example, we evaluate the expression (n1 + n2) * 5 using operator precedence. Here,
Code Example 2: Using operator precedence to solve an expression of relational operators in C.
Output:
Explanation:
In this example, we evaluate the expression of relational operators with operator precedence. Here,
The operator associativity in C is used when there exist two operators of the same precedence in an expression. The operator associativity in C can be either Left to Right or Right to Left. Associativity means the way operators are handled in an expression when two operators with the same precedence appear. For example, let’s say we have an expression: 8 / 4 * 5. Here, the expression has both operators of the same precedence, that is, division and multiplication. Therefore, we will use the operator associativity in C to evaluate the expression for finding the correct result.
We will now evaluate the following expression using the operator associativity:
Expression:
Here, the multiplication and division operator has the same operator precedence. Therefore, we will use the operator associativity to evaluate this expression. The associativity of multiplication and division is from Left to Right. Therefore, 8 / 4 is evaluated first, resulting in 2, and then 2 * 5 is evaluated to give 10.
So, associativity is only useful when two operators in an expression have the same precedence.
Code Example:
Output:
Explanation:
In this example, we are evaluating the expression a / b * c using the operator associativity. Here, as both division and multiplication operators have the same precedence, therefore the order of associativity will be used. The order of associativity of both operators is from Left to Right. After the evaluation is done, the result becomes 10 after all operations.
The given table represents the operator precedence from highest to lowest and their associativity in C language:
Precedence  Operators  Description  Associativity 
1  () [] > . 
Parentheses Square brackets Structure pointer Dot operator 
Left to Right 
2  ! ~ ++ – + – * & sizeOf (type) 
Logical NOT Bitwise complement Increment Decrement Unary plus Unary minus Dereference operator AddressOf operator Size in bytes Cast operator 

3  * / % 
Multiplication Division Modulus 
Left to Right 
4  #ERROR!  Addition Subtraction 
Left to Right 
5  << >> 
Bitwise shift left Bitwise shift right 
Left to Right 
6  < <= > >= 
Relational less than Relational less than or equal Relational greater than Relational greater than or equal 
Left to Right 
7  #ERROR!  Relational is equal to Relational is not equal to 
Left to Right 
8  &  Bitwise AND  Left to Right 
9  ^  Bitwise exclusive OR  Left to Right 
10    Bitwise inclusive OR  Left to Right 
11  &&  Logical AND  Left to Right 
12    Logical OR  Left to Right 
13  ?:  Ternay conditional  Right to Left 
14  #ERROR!  Assignment Addition assignment Subtraction assignment Multiplication assignment Division assignment Modulus assignment Bitwise shift left assignment Bitwise shift right assignment Bitwise AND assignment Bitwise exclusive assignment 
Right to Left 
15  ,  Comma operator  Left to Right 
Example of Operator Precedence and Associativity
We will now see a detailed and comprehensive example of operator precedence and associativity in an expression in C. Consider the following expression:
In the given expression, we have used the different operators in an expression including multiplication, division, subtraction, and addition. As we can see in the expression, division and multiplication have higher precedence or priority compared to addition and subtraction. Let’s now understand this example expression in more detail:
Therefore, after adding the parenthesis, the expression becomes:
As per the rules of associativity, the expression will be evaluated as:
Example:
Output:
1. Misunderstanding Parenthesis
The most common pitfall in expression evaluation is the misunderstanding of the logical and relational operator’s precedences. It must be used carefully. Let’s understand this with an example:
In this example, the relational operators (<) have higher precedence than the logical AND (&&), so x < y and y < z are evaluated first before the logical AND operation.
2. Incorrect usage of associativity
When working with operators like conditional and assignment, the associativity is assumed incorrectly. Let’s see how:
In this example, as the assignment operators have righttoleft associativity, 20 is assigned to z first, followed by y receiving the value of z (20), and x receiving the value of y (20).
3. Relying just on operator precedence without considering the associativity.
4. Misuse of similar symbols and different operators.
While working with the expressions in the C program, you must be aware of the operator precedence and their associativity. But there are some important points that everyone must remember while working with this:
In conclusion, operator precedence and associativity are the core elements in C programming. It’s about getting the order right when it comes to operators within expressions: a key task if you want to write code that does what you intend it to do and does it efficiently without any bugs. This article delves into some important points regarding this topic. Mastering the concept of operator precedence will empower programmers to produce not just any code but clear, efficient, and maintainable one – be it while dealing with simple arithmetic or complex algorithms. Always bear in mind that applying these rules guarantees that expressions are evaluated as expected.
This, in turn, adds up towards ensuring the reliability and correctness of C programs down the road for you as a developer gaining more experience – having an understanding of this would be helpful along your journey as a proficient C programmer.
Book a free counselling session
Get a personalized career roadmap
Get tailored program recommendations
Explore industry trends and job opportunities
Programs tailored for your success
Popular
Data Science
Technology
Finance
Management
Future Tech
© 2024 Hero Vired. All rights reserved