Chapter 09Beginner

Operator Precedence & Associativity

Understand which operators are evaluated first. Learn the PUMAS REBL TAC memory trick and when to use parentheses.

12 min readUpdated 2024-12-16
precedenceassociativityorder of operationsparenthesesPUMAS REBL TAC

What You Will Learn

  • Know which operators execute first
  • Understand left-to-right vs right-to-left
  • Use parentheses to control order
  • Memorize PUMAS REBL TAC mnemonic

01Why Precedence Matters

When you write an expression like 10 + 20 * 30, the compiler needs rules to decide which operation to perform first. Without these rules, different compilers could give different results!

❌ Without Precedence Rules

10 + 20 * 30

= (10 + 20) * 30?

= 900

✓ With Precedence Rules

10 + 20 * 30

= 10 + (20 * 30)

= 610

📝 This program proves that * is evaluated before + due to higher precedence.

why_precedence.c
C
1#include <stdio.h>
2
3int main() {
4 int result = 10 + 20 * 30;
5 printf("10 + 20 * 30 = %d\n", result);
6 // Output: 610, not 900!
7
8 return 0;
9}

02What is Operator Precedence?

Operator Precedence is a ranking that determines which operator is evaluated first when multiple operators appear in an expression. Operators with higher precedence are evaluated before those with lower precedence.

Precedence in Action

6+3*4/2
Step 1: * and / have equal precedence, evaluated left-to-right
6+12/2
6+6
Step 2: + is evaluated last
= 12

📝 This program shows step-by-step how precedence affects a complex expression.

precedence_example.c
C
1#include <stdio.h>
2
3int main() {
4 // Expression: 6 + 3 * 4 / 2
5 // Step 1: 3 * 4 = 12 (* evaluated first)
6 // Step 2: 12 / 2 = 6 (/ next, same precedence as *)
7 // Step 3: 6 + 6 = 12 (+ evaluated last)
8
9 int result = 6 + 3 * 4 / 2;
10 printf("6 + 3 * 4 / 2 = %d\n", result); // 12
11
12 // Compare with parentheses
13 int result2 = (6 + 3) * 4 / 2;
14 printf("(6 + 3) * 4 / 2 = %d\n", result2); // 18
15
16 return 0;
17}

03What is Associativity?

Associativity determines the order of evaluation when operators have the same precedence. It can be either:

Left-to-Right →

Most operators

100 / 5 % 2

= (100 / 5) % 2

= 20 % 2

= 0

← Right-to-Left

Assignment, unary, ternary

a = b = c = 5

= a = (b = (c = 5))

c=5, b=5, a=5

All = 5

📝 This program demonstrates both left-to-right and right-to-left associativity.

associativity.c
C
1#include <stdio.h>
2
3int main() {
4 // Left-to-right associativity (/, %, -, +)
5 int result1 = 100 / 5 % 2;
6 printf("100 / 5 %% 2 = %d\n", result1); // 0
7 // Evaluated as: (100 / 5) % 2 = 20 % 2 = 0
8
9 int result2 = 20 - 5 - 3;
10 printf("20 - 5 - 3 = %d\n", result2); // 12
11 // Evaluated as: (20 - 5) - 3 = 15 - 3 = 12
12 // NOT: 20 - (5 - 3) = 20 - 2 = 18
13
14 // Right-to-left associativity (=)
15 int a, b, c;
16 a = b = c = 10;
17 printf("a = b = c = 10: a=%d, b=%d, c=%d\n", a, b, c);
18 // Evaluated as: a = (b = (c = 10))
19
20 // Right-to-left for unary operators
21 int x = 5;
22 int y = - - x; // Double negation
23 printf("- - 5 = %d\n", y); // 5
24
25 return 0;
26}
Output

100 / 5 % 2 = 0

20 - 5 - 3 = 12

a = b = c = 10: a=10, b=10, c=10

- - 5 = 5

04Complete Precedence Table

Here is the complete operator precedence table from highest (1) to lowest (15) priority:

PriorityOperatorsDescriptionAssociativity
1() [] . -> ++ --Postfix operatorsLeft → Right
2++ -- + - ! ~ (type) * & sizeofUnary operatorsRight → Left
3* / %MultiplicativeLeft → Right
4+ -AdditiveLeft → Right
5<< >>Bitwise shiftLeft → Right
6< <= > >=RelationalLeft → Right
7== !=EqualityLeft → Right
8&Bitwise ANDLeft → Right
9^Bitwise XORLeft → Right
10|Bitwise ORLeft → Right
11&&Logical ANDLeft → Right
12||Logical ORLeft → Right
13?:Ternary conditionalRight → Left
14= += -= *= /= %= &= ^= |= <<= >>=AssignmentRight → Left
15,CommaLeft → Right

💡 Quick Reference

Highest: Postfix () [] → Lowest: Comma ,
Right-to-Left: Unary, Ternary, Assignment operators

05Memory Trick: PUMAS REBL TAC

Remembering 15 precedence levels is hard! Use this mnemonic to remember the order from highest to lowest:

PUMAS'REBL TAC

P

Postfix

() [] ++ --

U

Unary

++ -- ! ~ sizeof

M

Multiplicative

* / %

A

Additive

+ -

S

Shift

<< >>

R

Relational

< <= > >=

E

Equality

== !=

B

Bitwise

& ^ |

L

Logical

&& ||

T

Ternary

?:

A

Assignment

= += -= ...

C

Comma

,

🎯 Think of it as: "PUMA'S REBEL CAT"

Imagine a rebellious puma cat! This silly image will help you remember the order of operator precedence.

06Using Parentheses

Parentheses () have the highest precedence. Use them to override default precedence and make your code clearer.

📝 This program shows how parentheses change the evaluation order of the same numbers.

parentheses.c
C
1#include <stdio.h>
2
3int main() {
4 // Same numbers, different results!
5 int a = 100 + 200 / 10 - 3 * 10;
6 int b = (100 + 200) / 10 - 3 * 10;
7 int c = (100 + 200) / (10 - 3) * 10;
8 int d = 100 + 200 / (10 - 3 * 10);
9
10 printf("100 + 200 / 10 - 3 * 10 = %d\n", a); // 90
11 printf("(100 + 200) / 10 - 3 * 10 = %d\n", b); // 0
12 printf("(100 + 200) / (10 - 3) * 10 = %d\n", c); // 420
13 printf("100 + 200 / (10 - 3 * 10) = %d\n", d); // Division by -20
14
15 return 0;
16}

💡 Best Practice

Even when not required, use parentheses to make complex expressions clearer.(a && b) || c is clearer than a && b || c

07Common Pitfalls

❌ Chaining Comparisons (C vs Python)

main.c
C
int a = 10, b = 20, c = 30;
// This does NOT work like Python!
if (c > b > a) { // WRONG interpretation!
printf("TRUE");
}
// Evaluated as: ((c > b) > a) = (1 > 10) = 0 (FALSE)
// Correct way:
if (c > b && b > a) {
printf("TRUE"); // This works!
}

❌ Comma Operator Confusion

main.c
C
int a;
a = 1, 2, 3; // a = 1 (not 3!)
// Evaluated as: (a = 1), 2, 3
// What you probably meant:
a = (1, 2, 3); // a = 3 (last value)

❌ Bitwise vs Logical Operators

main.c
C
int x = 1, y = 2;
// & has LOWER precedence than ==
if (x & y == 0) { } // Parsed as: x & (y == 0)
// What you meant:
if ((x & y) == 0) { } // Use parentheses!

❌ Pre vs Post Increment in Expressions

main.c
C
int i = 5;
int a = i++ + ++i; // Undefined behavior!
// Don't modify a variable twice in one expression

08Summary

What You Learned:

  • Precedence: Determines which operator is evaluated first
  • Associativity: Order when operators have same precedence
  • PUMAS REBL TAC: Memory trick for precedence order
  • Parentheses: Highest precedence, use for clarity
  • Common Pitfalls: Chaining, comma, bitwise vs logical

09Next Steps

Now that you understand how expressions are evaluated, learn to control program flow: