View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

A Complete Guide to Operator Precedence and Associativity in C

Updated on 30/04/20252,736 Views

If you've ever written a mathematical expression in C and scratched your head wondering why the result wasn’t what you expected, you’re not alone. This confusion often stems from not fully understanding Operator Precedence and Associativity in C. These two concepts dictate how expressions are evaluated and in what order operations are performed. That’s why you’ll find this concept in all top-rated software development courses

In this blog, we’ll break down Operator precedence in C and Operator Associativity in C in a clear, conversational way with simple code examples. By the end, you'll not only understand how the C compiler evaluates complex expressions, but also how to use Operator Precedence and Associativity in C to your advantage.

What is Operator Precedence in C

Before you start with operator precedence in C, you should explore our following articles to strengthen your foundational knowledge: 

In C programming, expressions often involve multiple operators. But how does the compiler know which operation to perform first? That’s where operator precedence in C comes in. It defines the priority level of operators—deciding which operator is evaluated before others in a complex expression. 

Think of it like solving a math problem: you multiply before you add. In the same way, C follows a hierarchy. For instance, the multiplication operator `*` has higher precedence than the addition operator `+`. So, in an expression like `3 + 4 * 5`, the multiplication happens first, not the addition.

Let’s break it down with a code example.

Example: Demonstrating Operator Precedence in C

#include <stdio.h>

int main() {
    int result;

    // Example of Operator Precedence: * has higher precedence than +
    result = 3 + 4 * 5;

    printf("Result: %d\n", result);
    return 0;
}

Output:

Result: 23

Explanation:

  • The expression `3 + 4 * 5` is evaluated as `3 + (4 * 5)` due to operator Precedence in C.
  • Multiplication (`*`) happens first: `4 * 5 = 20`
  • Then addition: `3 + 20 = 23`

This is a simple but powerful example of how operator precedence in C directly affects the result of your expressions.

Unlock a high-paying opportunity at a Fortune 500 with following full-stack development courses: 

What is Operator Associativity in C

When multiple operators of the same precedence appear in an expression, how does the compiler determine the order of evaluation? That’s exactly what Operator Associativity in C helps clarify. It defines the direction in which operators of the same precedence level are evaluated—either left-to-right or right-to-left.

For instance, in the expression `a - b + c`, both `-` and `+` have the same precedence. Because these operators are left-associative, the expression is evaluated as `(a - b) + c`. If they were right-associative, it would be evaluated as `a - (b + c)` instead.

Let’s make this clearer with a code example.

Example: Demonstrating Operator Associativity in C

#include <stdio.h>

int main() {
    int a = 10, b = 5, c = 2, result;

    // + and - have the same precedence and left-to-right associativity
    result = a - b + c;

    printf("Result: %d\n", result);
    return 0;
}

Output:

Result: 7

Explanation:

  • `a - b + c` becomes `(10 - 5) + 2` because both `-` and `+` are left-associative.
  • `10 - 5 = 5`, then `5 + 2 = 7`

Understanding operator Associativity in C helps ensure expressions are evaluated the way you intend, especially when chaining multiple operators of equal precedence.

Table of Operator Precedence and Associativity in C

To master Operator Precedence and Associativity in C, it's crucial to have a reliable reference. Below is a structured table that shows the precedence levels of operators along with their associativity. Operators higher in the table have higher precedence.

Precedence Level

Operators

Description

Associativity

1 (Highest)

(), [], ->, .

Function call, array subscripting, structure/union member access

Left to Right

2

++, --, +, -, !, ~, *, &, sizeof, typecast

Unary operators, type cast, dereference, address-of, sizeof

Right to Left

3

*, /, %

Multiplication, division, modulo

Left to Right

4

+, -

Addition, subtraction

Left to Right

5

<<, >>

Bitwise shift left, shift right

Left to Right

6

<, <=, >, >=

Relational operators

Left to Right

7

==, !=

Equality operators

Left to Right

8

&

Bitwise AND

Left to Right

9

^

Bitwise XOR

Left to Right

10

`

`

Bitwise OR

11

&&

Logical AND

Left to Right

12

`

`

13

?:

Ternary conditional

Right to Left

14

=, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, `

=`

Assignment operators

15 (Lowest)

,

Comma operator

Left to Right

This table is an essential part of understanding operator precedence in C and operator Associativity in C. Keep it handy whenever you’re writing or debugging complex expressions.

Example of Operator Precedence in C

Let’s explore the top 5 example of operator Precedence in C

Example 1: Understanding Multiplication Before Addition

This example shows how multiplication takes precedence over addition in a simple arithmetic expression. To develop such programs efficiently, you should learn about the structure of a C program

#include <stdio.h>

int main() {
    int result;

    // * has higher precedence than +
    result = 2 + 3 * 4;

    printf("Result: %d\n", result);
    return 0;
}

Output:

Result: 14

Explanation:

  • According to Operator Precedence in C, the multiplication operator (`*`) has a higher priority than addition (`+`).
  • So, the expression `2 + 3 * 4` is evaluated as `2 + (3 * 4)`.
  • First, `3 * 4 = 12`, then `2 + 12 = 14`.
  • If you expected `5 * 4 = 20`, that would only happen if parentheses were used like `(2 + 3) * 4`.

Example 2: Changing Precedence Using Parentheses

Parentheses allow you to override default precedence and control the order of evaluation.

#include <stdio.h>

int main() {
    int result;

    // Parentheses force addition before multiplication
    result = (2 + 3) * 4;

    printf("Result: %d\n", result);
    return 0;
}

Output:

Result: 20

Explanation:

  • Here, the parentheses cause the addition to happen before multiplication.
  • `2 + 3 = 5`, then `5 * 4 = 20`.
  • Without parentheses, it would have followed normal Operator Precedence in C, where multiplication comes first.

Example 3: Arithmetic vs Relational Operators

This example shows how arithmetic operators take precedence over relational operators like `<`.

#include <stdio.h>

int main() {
    int result;

    // + is evaluated before <
    result = 5 + 2 < 10;

    printf("Result: %d\n", result);
    return 0;
}

Output:

Result: 1

Explanation:

  • `+` has higher precedence than `<`, so `5 + 2` is evaluated first.
  • `5 + 2 = 7`, then `7 < 10` is evaluated.
  • Since `7` is less than `10`, the result is `1` (true in C).

Example 4: Assignment vs Multiplication Precedence

Here, we see how assignment has lower precedence than arithmetic operations.

#include <stdio.h>

int main() {
    int a = 5, b = 2, result;

    // * is evaluated before =
    result = a * b + 1;

    printf("Result: %d\n", result);
    return 0;
}

Output:

Result: 11

Explanation:

  • First, multiplication: `a * b = 5 * 2 = 10`
  • Then addition: `10 + 1 = 11`
  • Finally, `result = 11`
  • Even though there's an assignment, the expression on the right is fully evaluated first due to Operator Precedence in C.

Example 5: Combining Multiple Operators

This example demonstrates how C handles a combination of arithmetic, relational, and logical operators based on precedence.

#include <stdio.h>

int main() {
    int a = 4, b = 5, c = 6;
    int result;

    // Arithmetic before relational, relational before logical
    result = a + b > c && c > b;

    printf("Result: %d\n", result);
    return 0;
}

Output:

Result: 1

Explanation:

  • Step-by-step evaluation due to Operator Precedence and Associativity in C:

  1. Arithmetic: `a + b = 4 + 5 = 9`

  2. Relational: `9 > 6` → true (`1`), and `6 > 5` → true (`1`)

  3. Logical AND (`&&`): `1 && 1 = 1` (both conditions true)

  • The final result is `1`, showing how precedence allows nested operations to be evaluated in the correct order.

These examples highlight how operator precedence in Censures consistent and predictable evaluation. Mastering this can help you avoid parentheses overuse, debug faster, and write cleaner code. By combining multiple operators, you can also create a calculator. For its in-depth insights, explore our article on creating calculator program in C

Examples of Operator Associativity in C

Now that we have covered Operator Precedence in C, let’s explore Operator Associativity in C. Associativity comes into play when operators with the same precedence level are involved. It dictates whether operators are evaluated from left to right or right to left. 

Let’s dive into some examples to understand this concept more clearly.

Example 1: Left-to-Right Associativity for Arithmetic Operators

Arithmetic operators like `+`, `-`, `*`, and `/` follow left-to-right associativity in C. This means that when you have multiple operators of the same precedence, the expression is evaluated from left to right.

#include <stdio.h>

int main() {
    int result;

    // + and - have left-to-right associativity
    result = 10 - 5 + 3;

    printf("Result: %d\n", result);
    return 0;
}

Output:

Result: 8

Explanation: 

  • Both `-` and `+` operators have the same precedence, and they are left-associative.
  • First, `10 - 5 = 5`, and then `5 + 3 = 8`.
  • So, the result is `8`.

Example 2: Right-to-Left Associativity for Assignment Operators

Assignment operators (`=`, `+=`, `-=`, etc.) have right-to-left associativity. This means that in an expression involving multiple assignment operators, the rightmost assignment is performed first. 

#include <stdio.h>

int main() {
    int a, b, c;

    // Assignment operators have right-to-left associativity
    a = b = c = 10;

    printf("a = %d, b = %d, c = %d\n", a, b, c);
    return 0;
}

Output:

a = 10, b = 10, c = 10

Explanation: 

  • The assignment operator is right-associative, so `c = 10` is executed first.
  • Then, `b = c` becomes `b = 10`, and finally `a = b` becomes `a = 10`.
  • Hence, all three variables are assigned the value `10`.

Example 3: Left-to-Right Associativity in Logical AND and OR

Logical operators like `&&` (AND) and `||` (OR) also follow left-to-right associativity in C.

#include <stdio.h>

int main() {
    int result;

    // && has left-to-right associativity
    result = 1 && 0 && 1;

    printf("Result: %d\n", result);
    return 0;
}

Output:

Result: 0

Explanation: 

  • `&&` (AND) is left-to-right associative.
  • First, `1 && 0` is evaluated, which is `0`.
  • Then, `0 && 1` is evaluated, which is also `0`.
  • So, the result of the entire expression is `0`.

Example 4: Left-to-Right Associativity in Comparison Operators

Comparison operators like `==`, `!=`, `<`, `<=`, `>`, and `>=` also follow left-to-right associativity.

#include <stdio.h>

int main() {
    int result;

    // Comparison operators have left-to-right associativity
    result = 5 < 10 == 5;

    printf("Result: %d\n", result);
    return 0;
}

Output:

Result: 1

Explanation: 

  • Since comparison operators are left-to-right associative, the expression `5 < 10 == 5` is evaluated as `(5 < 10) == 5`.
  • `5 < 10` is `true` (which is `1` in C), and then `1 == 5` is `false` (which is `0`).
  • Therefore, the final result is `0`, but this behavior emphasizes how associativity can impact the order of evaluation.

Example 5: Right-to-Left Associativity for the Ternary Operator

The ternary conditional operator (`?:`) in C has right-to-left associativity. This means when you have nested ternary operators, the evaluation starts from the rightmost operator.

#include <stdio.h>

int main() {
    int result;

    // Ternary operator has right-to-left associativity
    result = (5 < 10) ? (10 < 20 ? 1 : 0) : 2;

    printf("Result: %d\n", result);
    return 0;
}

Output:

Result: 1

Explanation: 

  • The ternary operator is right-associative, so the expression is evaluated starting from the rightmost part.
  • First, the inner ternary `(10 < 20 ? 1 : 0)` is evaluated. Since `10 < 20` is true, it evaluates to `1`.
  • Then, the outer ternary checks `5 < 10`, which is true, so the result becomes `1`.
  • The result of the entire expression is `1`.

These examples showcase how operator associativity in C dictates the order in which operators with the same precedence are evaluated. Understanding this concept can help you write clearer and more accurate expressions, especially when dealing with complex statements that involve multiple operators.

Also learn about compiling C program in Linux, and about C compiler in Mac to develop programs on any operating system. 

Operator Precedence and Associativity in C: Key Points To Remember

Operator Precedence in C

Operator Precedence dictates the order in which operators are evaluated. Operators with higher precedence are evaluated first. For example, multiplication (`*`) is performed before addition (`+`). Parentheses can be used to explicitly control the evaluation order and override the default precedence.

Operator Associativity in C  

Operator Associativity determines the direction of evaluation for operators of the same precedence. Most operators follow left-to-right associativity, meaning they are evaluated from left to right. However, assignment operators (`=`, `+=`, etc.) follow right-to-left associativity, meaning they are evaluated from right to left.

Use Parentheses for Clarity  

Although Operator Precedence and Associativity dictate evaluation order, using parentheses ensures clarity and eliminates ambiguity in complex expressions. Parentheses help specify the evaluation order explicitly, making the code more readable and predictable.

Caution with Assignment Operators  

Assignment operators have right-to-left associativity, so expressions like `a = b = c = 10` are evaluated from right to left. Understanding this behavior is essential to avoid unexpected results when using multiple assignments.

Know the Precedence of Operators  

Knowing the precedence of operators ensures correct evaluation. For example, the postfix increment (`++`) and dereference (`*`) operators have the highest precedence and are evaluated before other operators. Understanding this helps in writing more predictable and error-free code.

Conclusion

Understanding Operator Precedence and Associativity in C is vital for writing efficient and bug-free code. These two concepts determine how expressions are evaluated and can impact the logic of your program. By knowing the precedence rules, you can predict how operators interact in complex expressions, while understanding associativity ensures the correct order when operators have the same precedence.

Using parentheses is a good practice to make the evaluation order explicit, reducing ambiguity and improving the readability of your code. Special attention should be given to right-to-left associativity in assignment and ternary operators, as these can easily lead to unexpected results if not handled correctly.

Mastering Operator Precedence in C and Operator Associativity in C allows you to write cleaner, more reliable programs and avoid logical errors, helping you become a more proficient C programmer.

FAQs

1. What is operator precedence in C?

Operator precedence in C determines the order in which operators are applied in an expression. For example, multiplication and division have higher precedence than addition and subtraction, so they are evaluated first. Understanding precedence helps avoid errors in expressions where multiple operators are involved. However, precedence alone isn't always enough; associativity also plays a crucial role.

2. How does operator associativity work in C?

Operator associativity in C governs how operators of the same precedence are evaluated. Most operators, such as arithmetic and logical operators, follow left-to-right associativity, meaning they are evaluated in the order they appear from left to right. However, some operators like assignment (`=`) and the ternary conditional (`?:`) follow right-to-left associativity, meaning they are evaluated from right to left.

3. Why are parentheses important in C expressions?

Parentheses in C are used to override the default precedence of operators. By explicitly defining the order of operations within parentheses, you ensure that complex expressions are evaluated in the desired order. This reduces the risk of errors, especially in cases where operator precedence and associativity could lead to unexpected results without clear parentheses.

4. What happens if operator precedence is not considered?

If operator precedence is ignored, an expression may produce unintended results. For example, "2 + 3 * 4" is evaluated as "2 + (3 * 4)" because multiplication has higher precedence than addition. Not considering precedence may lead to logic errors, causing the program to behave differently than expected and possibly leading to incorrect calculations or outcomes.

5. How do assignment operators work in C?

In C, assignment operators like `=`, `+=`, and `-=` are used to assign values to variables. These operators have right-to-left associativity. This means that in an expression like "a = b = c = 10", the assignment starts with `c = 10`, then `b = c`, and finally `a = b`. Understanding this behavior is essential for avoiding confusion when multiple assignments are involved in a single line of code.

6. Can I mix operators with different precedence?

Yes, operators with different precedence can be mixed in the same expression. However, the result depends on the precedence rules. Operators with higher precedence are evaluated first. For example, in the expression "2 + 3 * 4", multiplication is performed before addition because `*` has higher precedence than `+`. To ensure the correct evaluation order, parentheses are often used.

7. How does the ternary operator work in C?

The ternary operator `?:` is a shorthand for an if-else statement and follows right-to-left associativity. It evaluates the condition first, then returns one of two values based on whether the condition is true or false. For example, `x = (a > b) ? a : b` assigns the greater of `a` and `b` to `x`. It's an efficient way to perform conditional assignments but requires understanding associativity.

8. Can associativity affect the outcome of an expression?

Yes, associativity can affect the outcome of an expression, especially when multiple operators of the same precedence are used. For example, in an expression like "a = b = c = 10", the assignment happens from right to left because the assignment operator has right-to-left associativity. Without understanding this behavior, it might seem like the assignments should occur from left to right, leading to potential errors.

9. How do logical operators work with precedence and associativity?

Logical operators like AND (`&&`) and OR (`||`) follow **left-to-right associativity** and have lower precedence than arithmetic and relational operators. This means that in an expression with mixed operators, such as "x > 5 && y < 10", the relational operators are evaluated first, followed by the logical AND. The evaluation follows the order of precedence and associativity, ensuring accurate results.

10. What role does the comma operator play in expressions?

The comma operator `,` allows multiple expressions to be evaluated in sequence within a single statement. It has very low precedence, meaning it is evaluated last in an expression. The value of the entire expression is the value of the last expression evaluated. For example, in `a = (x++, y++)`, the comma operator ensures that both increments happen before the assignment, but only `y++` is assigned to `a`.

11. How does precedence impact complex expressions?

In complex expressions with multiple operators, precedence plays a crucial role in determining the order of evaluation. Without proper understanding, it’s easy to make logical mistakes. For example, in an expression like `x + y * z`, multiplication is performed first because it has higher precedence than addition. To avoid such issues, developers can use parentheses to explicitly define the order of operations, preventing unintended results.

image

Take a Free C Programming Quiz

Answer quick questions and assess your C programming knowledge

right-top-arrow
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
advertise-arrow

Free Courses

Start Learning For Free

Explore Our Free Software Tutorials and Elevate your Career.

upGrad Learner Support

Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918068792934

Disclaimer

1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.

2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.