Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Operator Precedence in C
3.
Operator Associativity in C
4.
Key Points About Operator Precedence and Associativity in C.
4.1.
When two or more operators have the same precedence, associativity is utilized.
4.1.1.
Example
4.2.
All operators with the same precedence have the same associativity. 
4.3.
Comma has the least precedence. 
4.3.1.
Example
4.4.
Postfix Increment and Prefix Increment have different Precedence and Associativity.
4.5.
No comparison chaining of operators in C 
4.5.1.
Example
5.
Operator Precedence and Associativity Table
6.
Frequently Ask Questions
6.1.
Which of the operators has higher precedence, the addition or subtraction?
6.2.
Which of the operators has higher precedence, multiplication or division?
6.3.
Which operator has the lowest precedence?
7.
Conclusion
Last Updated: Mar 27, 2024

Operator Precedence and Associativity in C

Author Rhythm Jain
1 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Working with operators can be confusing, especially when we are working with multiple operators simultaneously. So to avoid any misunderstanding, we need to know Operator Precedence and Associativity work in C. 

Also See, Sum of Digits in C and Data Structure

Operator Precedence in C

Operator precedence controls how terms in an expression are grouped and how an expression is evaluated. Certain operators take precedence over others. The multiplication operator, for example, takes priority over the addition operator.

For example, x = 2 + 3 * 5; 

Here, the value of x will be assigned as 17 and not 20. The “ * ”operator has higher precedence than the “ + ” operator. So the first 3 is multiplied by 5 to get 15, and then 2 is added to 15 to result in 17.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Operator Associativity in C

The direction in which an expression is evaluated is determined by the associativity of operators. Associativity is utilized when two operators of the same precedence exist in an expression. Associativity can be either left to right or right to left.

For example, consider x = 5 / 3 * 3; 

Here, the value of x will be assigned as 3 and not 5. ‘*’ operator and ‘/’ operator have the same precedence, but their associativity is from Left to Right. So first 5 is divided by 3 to get 1, and then 1 is multiplied by 3, resulting in 3.

Key Points About Operator Precedence and Associativity in C.

When two or more operators have the same precedence, associativity is utilized.

Example

#include <stdio.h>


int main()
{


    printf("Output of 1 == 2 != 3 is %d",1 == 2 != 3);
    
    return 0;
}

Output:

Output of 1 == 2 != 3 is 1

In this case, the operators ‘==’ and ‘!=’ have the same priority. And they are related from left to right. As a result, 1 == 2 is executed first.

Also see, Floyd's Triangle in C

All operators with the same precedence have the same associativity. 

This is required because otherwise, the compiler would be unable to determine the evaluation order of phrases with two operators with the same precedence but differing associativity. For example, the associativity of + and – is the same.

Comma has the least precedence. 

The comma has the least precedence among all operators and should be used with caution.

Example

#include <stdio.h>
int main()
{
    int a;
    a = 10, 20, 30; 
    printf("%d", a);
    return 0;
}

Output:

10

Here /the expression is Evaluated as (a = 1), 2, 3.

Postfix Increment and Prefix Increment have different Precedence and Associativity.

Postfix ++ has more precedence than prefix ++, and their associativity is also different. The associativity of postfix ++ is left to right, while that of prefix ++ is right to left.

No comparison chaining of operators in C 

In Python, an expression such as "c > b > a" is handled as "c > b and b > a," while in C, this sort of chaining does not occur.

Example

#include <stdio.h>
int main()
{
    int x = 1, y = 2, z = 3;
    if (x > y > z)
        printf("True");
    else
        printf("False");
    return 0;
}

Output:

False

Here (x > y > z) is treated as ((x  > y) > z) because associativity of '>' is from left to right. Therefore the expression becomes ((30 > 20) > 10) which becomes (1 > 10) which results in false condition.

You can practice this code on the online C editor.

Operator Precedence and Associativity Table

In this table, operators with the highest precedence appear at the top, while those with the lowest appear at the bottom. Higher precedence operators in an expression will be evaluated first.


Also see : C Static Function and  Short int in C Programming

Frequently Ask Questions

Which of the operators has higher precedence, the addition or subtraction?

The precedence of Addition and Subtraction arithmetic operators is the same. So, let’s say we have an expression with us which is 20 + 5 – 10. The evaluation of this expression would be (20 + 5) – 10 because the associativity will be left to right for both the operators – multiplication and division. In a similar case, the calculation of 25 – 10 would be 15 because the associativity would be from right to left here.

Which of the operators has higher precedence, multiplication or division?

The precedence of Division and Multiplication arithmetic operators is the same. So, let’s say we have an expression with us which is 6 * 3 / 20. The evaluation of this expression would be (6 * 3) / 20 because the associativity will be left to right for both the operators – multiplication and division. In a similar case, the calculation of 40 / 4 * 5 would be (40 / 4) * 5 because the associativity would be from right to left here.

Which operator has the lowest precedence?

Comma has the lowest precedence among all the operators.

Conclusion

In this blog, we have extensively discussed Operator Precedence and Associativity in C. Operator precedence dictates the order of evaluation of operators in an expression.

Recommended Reading:

Difference Between List and Set

Refer to our Coding Ninjas Studio Guided Path to learn Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and much more! If you want to put your coding skills to the test, check out the mock test series and enter the contests presented by Coding Ninjas Studio! But say you're just starting and want to learn about problems posed by tech titans like Amazon, Microsoft, Uber, etc. In such a case, for placement preparations, you must consider the challenges, interview experiences, and interview bundle.

Nonetheless, you may want to try our premium courses to give your job a competitive advantage!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Previous article
Indirection Operator (*) in C
Next article
operands for sizeof operator in C
Live masterclass