Lesson 22 of 60 – Operator Precedence in C
37%

Operator Precedence in C

Operator precedence determines the order in which operators are evaluated when an expression contains more than one operator. Understanding precedence helps you predict the result of an expression correctly.

Note: Operators with higher precedence are generally evaluated before operators with lower precedence. Parentheses can be used to explicitly control the order of evaluation.

1. What is Operator Precedence?

Operator precedence is the set of rules that determines which operator should be evaluated first in an expression.

int result = 10 + 5 * 2;

Multiplication has higher precedence than addition, so the expression is evaluated as:

10 + (5 * 2)

The result is:

20

2. Why Operator Precedence is Important

When an expression contains several operators, C needs rules to determine the order in which those operators are evaluated.

int result = 20 - 10 / 2;

Division is performed before subtraction:

20 - (10 / 2)

Therefore:

result = 15;

3. Basic Precedence Rule

A simple rule to remember is:

  1. Parentheses are handled first.
  2. Unary operators have high precedence.
  3. Multiplication, division, and remainder come before addition and subtraction.
  4. Relational operators are evaluated after arithmetic operators.
  5. Equality operators come after relational operators.
  6. Logical AND comes before logical OR.
  7. Assignment operators have relatively low precedence.

4. Parentheses Have High Precedence

int result = (10 + 5) * 2;

The expression inside parentheses is evaluated first:

(10 + 5) = 15

Then:

15 * 2 = 30

Therefore, the result is 30.

5. Multiplication Before Addition

int result = 5 + 4 * 3;

Multiplication is performed first:

4 * 3 = 12

Then:

5 + 12 = 17

So the result is 17.

6. Division Before Subtraction

int result = 30 - 20 / 5;

Division is evaluated first:

20 / 5 = 4

Then:

30 - 4 = 26

Therefore, the result is 26.

7. Remainder Operator Precedence

int result = 10 + 7 % 3;

The remainder operator has the same precedence level as multiplication and division.

7 % 3 = 1

Then:

10 + 1 = 11

So the result is 11.

8. Multiplication, Division and Remainder

The operators *, /, and % have the same precedence level.

int result = 20 / 5 * 3;

Operators with the same precedence are generally evaluated according to their associativity. These arithmetic operators are evaluated from left to right.

(20 / 5) * 3

Result:

12

9. Addition and Subtraction

The + and - arithmetic operators have the same precedence.

int result = 20 - 5 + 3;

They are evaluated from left to right:

(20 - 5) + 3

Result:

18

10. Left-to-Right Evaluation

When operators have the same precedence and the operators are left associative, evaluation proceeds from left to right.

int result = 100 / 10 * 2;

This becomes:

(100 / 10) * 2

Result:

20

11. Unary Operators

Unary operators work with one operand. Examples include:

  • ++
  • --
  • !
  • Unary -
  • Unary +
int x = 5;

int result = -x + 10;

The unary minus is applied to x before the addition.

12. Relational Operators

Relational operators include:

<   >   <=   >=

They are generally evaluated after arithmetic operations.

int result = 5 + 3 > 7;

The addition is performed first:

8 > 7

The condition is true, so the result is typically 1.

13. Equality Operators

Equality operators are:

==
!=

They have lower precedence than relational operators.

int result = 5 < 10 == 1;

The relational expression is evaluated first:

(5 < 10) == 1

This results in true, so result becomes 1.

14. Logical AND Operator

The logical AND operator is &&. It has lower precedence than equality and relational operators.

int result = 10 > 5 && 20 > 10;

The relational expressions are evaluated first:

(10 > 5) && (20 > 10)

Both conditions are true, so the result is 1.

15. Logical OR Operator

The logical OR operator is ||. It has lower precedence than logical AND.

int result = 10 > 5 || 20 < 10;

The comparisons are evaluated first:

(10 > 5) || (20 < 10)

Since the first condition is true, the result is 1.

16. Logical AND Before Logical OR

The logical AND operator has higher precedence than logical OR.

int result = 1 || 0 && 0;

The AND operation is evaluated first:

1 || (0 && 0)

Then:

1 || 0

The final result is 1.

17. Assignment Operators Have Low Precedence

Assignment operators such as =, +=, -=, and *= have relatively low precedence.

int x;

x = 10 + 5 * 2;

The arithmetic expression is evaluated first:

10 + (5 * 2)

Then the result is assigned to x.

18. Parentheses Can Change the Result

int result1 = 10 + 5 * 2;
int result2 = (10 + 5) * 2;

First expression:

10 + (5 * 2) = 20

Second expression:

(10 + 5) * 2 = 30

Parentheses make the intended order explicit.

19. Precedence vs Associativity

Precedence determines which operator is considered first when different precedence levels are involved.

Associativity determines the direction of evaluation when operators have the same precedence.

20 / 5 * 2

Both / and * have the same precedence and are left associative, so the expression is evaluated as:

(20 / 5) * 2

20. Conditional Operator Precedence

The conditional operator ?: has lower precedence than logical OR and higher precedence than assignment operators.

int result = 10 > 5 ? 100 : 200;

The relational comparison is evaluated before the conditional operator.

For readability, parentheses can be used:

int result = (10 > 5) ? 100 : 200;

21. Assignment with Conditional Operator

int age = 20;

int status = (age >= 18) ? 1 : 0;

The comparison is evaluated first, then the conditional operator selects the appropriate value, and finally the result is assigned to status.

22. Complex Expression Example

int result = 10 + 5 * 2 > 15;

Step 1: Multiplication:

5 * 2 = 10

Step 2: Addition:

10 + 10 = 20

Step 3: Comparison:

20 > 15

The result is 1.

23. Using Parentheses for Readability

Even when you know the precedence rules, parentheses can make an expression easier for other programmers to understand.

int result = (a + b) * (c - d);

This clearly shows that both additions and subtractions inside the parentheses should be completed before multiplication.

24. Common Precedence Order

A simplified order from higher to lower precedence is:

  1. Parentheses and postfix operators
  2. Unary operators
  3. Multiplication, division, remainder
  4. Addition and subtraction
  5. Relational operators
  6. Equality operators
  7. Logical AND
  8. Logical OR
  9. Conditional operator
  10. Assignment operators

This is a simplified learning order. The complete C operator precedence table contains additional operators and associativity rules.

25. Common Mistake: Ignoring Precedence

int result = 10 + 20 / 5;

A beginner may calculate it as:

(10 + 20) / 5

But that is not how C evaluates the expression.

Division has higher precedence:

10 + (20 / 5)

Result:

14

26. Common Mistake: Confusing = and ==

The assignment operator = is different from the equality operator ==.

int x = 10;

if (x == 10)
{
    printf("Equal");
}

Use = to assign a value and == to compare values.

27. Complete Example

#include <stdio.h>

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

    int result = a + b * c;

    printf("Result = %d", result);

    return 0;
}

The expression is evaluated as:

10 + (5 * 2)

Therefore:

Result = 20

28. Practical Example with User Input

#include <stdio.h>

int main()
{
    int a, b;

    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);

    int result = a + b * 2;

    printf("Result = %d", result);

    return 0;
}

Multiplication is performed before addition in the expression a + b * 2.

29. How to Avoid Precedence Mistakes

  • Learn the common operator precedence levels.
  • Use parentheses when the intended order is important.
  • Do not make expressions unnecessarily complicated.
  • Break complex calculations into smaller expressions.
  • Remember that same-precedence operators also follow associativity.
  • Test complex expressions with simple values.
int total = (price * quantity) + tax;

Parentheses make the calculation easier to understand.

30. Practice Questions

Find the result of each expression:

  1. 10 + 5 * 2
  2. 20 - 10 / 2
  3. (10 + 5) * 2
  4. 20 / 5 * 2
  5. 10 + 7 % 3
  6. 5 + 3 > 7
  7. 10 > 5 && 20 > 10
  8. 1 || 0 && 0
  9. 10 + 5 * 2 > 15
  10. (10 + 5) > 12 ? 100 : 200

Try to solve each expression step by step using operator precedence before checking the answer.

📌 Key Points

  • Operator precedence determines the order in which operators are evaluated.
  • Parentheses can be used to explicitly control evaluation order.
  • Multiplication, division, and remainder have higher precedence than addition and subtraction.
  • Operators with the same precedence follow their associativity rules.
  • Most arithmetic operators such as *, /, and % are left associative.
  • Relational operators are generally evaluated after arithmetic operators.
  • Equality operators have lower precedence than relational operators.
  • Logical AND has higher precedence than logical OR.
  • The conditional operator has lower precedence than logical OR and higher precedence than assignment.
  • Assignment operators have relatively low precedence.
  • Parentheses can improve both correctness and readability.

🧠 Quick Quiz

Question: Which operator is evaluated before the addition operator in the expression 10 + 5 * 2?