Table of contents
1.
Introduction
2.
What is Operator Precedence in C?
3.
What is 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: Sep 27, 2024

Operator Precedence and Associativity in C

Author Rhythm Jain
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Operator precedence and associativity are fundamental concepts in C programming. Precedence determines the order in which operators are evaluated, while associativity defines the direction of evaluation for operators with the same precedence. Knowledge of these two concepts is crucial for writing accurate expressions and avoiding logical errors. C provides a well-defined set of rules for operator precedence and associativity, allowing developers to create complex expressions with predictable outcomes. In this article we will discuss these in detail with proper code examples.

Also See, Sum of Digits in C 

What is 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.

What is 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;
}
You can also try this code with Online C Compiler
Run Code

Output:

Output of 1 == 2 != 3 is 1
You can also try this code with Online C Compiler
Run Code

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;
}
You can also try this code with Online C Compiler
Run Code

Output:

10
You can also try this code with Online C Compiler
Run Code

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;
}
You can also try this code with Online C Compiler
Run Code

Output:

False
You can also try this code with Online C Compiler
Run Code

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?

In most programming languages, including C, C++, and Python, the operator with the lowest precedence is typically the assignment operator (=).

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

In programming, multiplication (*) and division (/) operators have the same level of precedence, which is higher than addition and subtraction.

Which operator has the lowest precedence?

Addition (+) and subtraction (-) also share the same precedence level in programming. Their precedence is lower compared to multiplication and division.

Conclusion

In this article, we talked about operator precedence and associativity in C. We learned that precedence determines the order of operator evaluation, while associativity defines the direction of evaluation for operators with equal precedence. With the help of these concepts and by following C's well-defined rules, developers can write accurate expressions and ensure their code behaves as intended. We also discussed which operator has the highest and lowest precedence with proper table of operators.

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.

Live masterclass