Nearby lessons

34 of 124

C - Operator Precedence

Operator Precedence is one of the foundational topics in C programming. This lesson explains Operator Precedence — What Runs First with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Operator Precedence — What Runs First

When an expression has many operators, C follows a fixed priority order. The higher-precedence operator runs first. See it live in one program:

Example01
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 int x, y;
6 
7 x = 2 + 3 * 4; // 3*4 first = 12, then 2+12
8 printf("2 + 3 * 4 = %d\n", x); // 14
9 
10 y = (2 + 3) * 4; // brackets first: 5*4
11 printf("(2 + 3) * 4 = %d\n", y); // 20
12 
13 int z = 10 + 5 > 12; // 10+5 = 15, then 15 > 12
14 printf("10 + 5 > 12 = %d\n", z); // 1
15}
Output

2 + 3 * 4  = 14
(2 + 3) * 4 = 20
10 + 5 > 12 = 1
      

Operator Precedence — What Runs First

Trainer's Note: Trainer tip: never rely on memory for precedence. When in doubt, use brackets `( )` — they make your intention clear and avoid bugs. a + (b * c) is easier to read than hoping C does the right thing.
Example02
CCode Cell
1Priority (high to low):
21. ( ) brackets
32. * / %
43. + -
54. < <= > >=
65. == !=
76. &&
87. ||
98. = (assignment) - lowest
📝 Key Takeaways
  • Operators: arithmetic (+ - * / %), relational (== != < > <= >=), logical (&& || !), assignment (= += -= *= /= %=), ++ --, ternary ?:.
  • Each operator family has its own complete program — copy and run one at a time.
  • Integer division drops decimals: 5/2 = 2; use 5.0/2 for 2.5.
  • % gives the remainder and works only on integers.
  • a++ uses then increases; ++a increases then uses.
  • Ternary: condition ? value1 : value2.
  • Use brackets to control the order of evaluation.

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1