BCA Sem 1 | Unit 1 | Computer Concepts & C Programming
Operators & Hierarchy of Operators — BKNMU Junagadh
Complete Guide to Operator Precedence, Associativity, and Step-by-Step Expression Evaluation
a + b * c / d), the compiler follows a strict set of rules called the Operator Hierarchy (Precedence & Associativity) to determine which operation to execute first. Understanding this hierarchy ensures your calculations produce predictable, bug-free results.
Expression evaluation in C relies on two primary rules:
1. Operator Precedence
Determines the priority order of operators. Operators with higher precedence are evaluated before those with lower precedence.
2. Associativity
Determines the direction of evaluation (Left-to-Right or Right-to-Left) when two operators have equal precedence.
3. Parentheses Priority
Parentheses () have the highest precedence and can be used to override default evaluation order.
Evaluating result = 10 + 20 * 3;
Because * has higher precedence than +, multiplication happens before addition.
Here is the official C operator hierarchy table listed from highest priority (Rank 1) to lowest priority (Rank 15):
| Rank | Operator Category | Operator Symbols | Associativity |
|---|---|---|---|
1 | Postfix / Primary | (), [], ->, ., ++ (postfix), -- (postfix) | Left-to-Right |
2 | Unary | ++ (prefix), -- (prefix), +, -, !, ~, (type), *, &, sizeof | Right-to-Left |
3 | Multiplicative | *, /, % | Left-to-Right |
4 | Additive | +, - | Left-to-Right |
5 | Bitwise Shift | <<, >> | Left-to-Right |
6 | Relational | <, <=, >, >= | Left-to-Right |
7 | Equality | ==, != | Left-to-Right |
8 | Bitwise AND | & | Left-to-Right |
9 | Bitwise XOR | ^ | Left-to-Right |
10 | Bitwise OR | | | Left-to-Right |
11 | Logical AND | && | Left-to-Right |
12 | Logical OR | || | Left-to-Right |
13 | Conditional (Ternary) | ?: | Right-to-Left |
14 | Assignment | =, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>= | Right-to-Left |
15 | Comma | , | Left-to-Right |
1. Unary Operators (e.g.,
!, ++, sizeof)
2. Conditional Operator (
?:)
3. Assignment Operators (e.g.,
=, +=)
Let's evaluate a complex expression step-by-step as asked in university theory exams:
Expression: int x = 5 + 3 * 4 / 2 - 1;
- Multiplicative operators (
*,/) have equal rank (Rank 3). - Associativity is Left-to-Right, so
3 * 4happens first. - Result:
5 + 12 / 2 - 1
- Next, division is evaluated.
12 / 2evaluates to6.- Result:
5 + 6 - 1
- Additive operators (
+,-) are Rank 4 (Left-to-Right). 5 + 6 = 11, then11 - 1 = 10.- Final Result:
x = 10
Observe how parentheses alter default operator precedence in C code:
a == b && c > d, relational operators (==, >) are evaluated BEFORE logical AND (&&). So the expression is treated as (a == b) && (c > d).
Identify Parentheses First
Always solve innermost parentheses () first before outside operations.
Scan for Unary & Multiplicative Operators
Execute Unary (++, --, !) and then Multiplicative (*, /, %).
Apply Associativity for Tied Rank
If two operators share equal rank, apply evaluation direction (usually Left-to-Right).
Complete Assignment Last
Assign final calculated value to left-hand variable using = (Right-to-Left).
%), Multiplication (*), and Division (/) share the **exact same precedence level**. They are evaluated strictly from **Left to Right** as they appear!
- Precedence: Determines operator priority rank during expression evaluation.
- Associativity: Determines execution direction (L-to-R or R-to-L) when ranks tie.
- Highest Priority: Parentheses
()override all operator rules. - Right-to-Left Categories: Unary, Conditional (
?:), and Assignment (=,+=). - Arithmetic Tier:
* / %higher than+ -. - Logical Tier: Relational (
< > ==) higher than Logical (&& ||). - Lowest Priority: Comma operator (
,) has lowest precedence in C.