Operators & Hierarchy of Operators – BKNMU Junagadh | BCA Sem 1 Notes



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

BKNMU Junagadh NEP 2020 Operator Hierarchy Precedence Rules Associativity Direction
In simple words: When a C expression contains multiple operators (like 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.
📖 Precedence vs. Associativity Explained

Expression evaluation in C relies on two primary rules:

* / % BEFORE + -

1. Operator Precedence

Determines the priority order of operators. Operators with higher precedence are evaluated before those with lower precedence.

Left → Right

2. Associativity

Determines the direction of evaluation (Left-to-Right or Right-to-Left) when two operators have equal precedence.

(a + b) * c

3. Parentheses Priority

Parentheses () have the highest precedence and can be used to override default evaluation order.

🎨 Expression Evaluation at a Glance

Evaluating result = 10 + 20 * 3;

Step 1: Multiplication First (20 * 3 = 60) Step 2: Addition Next (10 + 60 = 70)

Because * has higher precedence than +, multiplication happens before addition.

1️⃣ Complete Operator Precedence & Associativity Table

Here is the official C operator hierarchy table listed from highest priority (Rank 1) to lowest priority (Rank 15):

RankOperator CategoryOperator SymbolsAssociativity
1Postfix / Primary(), [], ->, ., ++ (postfix), -- (postfix)Left-to-Right
2Unary++ (prefix), -- (prefix), +, -, !, ~, (type), *, &, sizeofRight-to-Left
3Multiplicative*, /, %Left-to-Right
4Additive+, -Left-to-Right
5Bitwise Shift<<, >>Left-to-Right
6Relational<, <=, >, >=Left-to-Right
7Equality==, !=Left-to-Right
8Bitwise AND&Left-to-Right
9Bitwise XOR^Left-to-Right
10Bitwise OR|Left-to-Right
11Logical AND&&Left-to-Right
12Logical OR||Left-to-Right
13Conditional (Ternary)?:Right-to-Left
14Assignment=, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=Right-to-Left
15Comma,Left-to-Right
ℹ️ Memory Trick for Exams: Remember the Right-to-Left operators! Only 3 categories evaluate from Right to Left:
1. Unary Operators (e.g., !, ++, sizeof)
2. Conditional Operator (?:)
3. Assignment Operators (e.g., =, +=)
🔍 Step-by-Step Expression Tracing Example

Let's evaluate a complex expression step-by-step as asked in university theory exams:

Expression: int x = 5 + 3 * 4 / 2 - 1;

Step 1: 3 * 4
  • Multiplicative operators (*, /) have equal rank (Rank 3).
  • Associativity is Left-to-Right, so 3 * 4 happens first.
  • Result: 5 + 12 / 2 - 1
Step 2: 12 / 2
  • Next, division is evaluated.
  • 12 / 2 evaluates to 6.
  • Result: 5 + 6 - 1
Step 3: 5 + 6 - 1
  • Additive operators (+, -) are Rank 4 (Left-to-Right).
  • 5 + 6 = 11, then 11 - 1 = 10.
  • Final Result: x = 10
💻 Code Sample — Testing Hierarchy in C

Observe how parentheses alter default operator precedence in C code:

#include <stdio.h> int main() { int a = 10, b = 20, c = 2; // Without parentheses: Multiplication happens first int res1 = a + b * c; // 10 + (20 * 2) = 50 // With parentheses: Addition happens first int res2 = (a + b) * c; // (10 + 20) * 2 = 60 printf("Result Without Brackets: %d\n", res1); printf("Result With Brackets: %d\n", res2); return 0; }
💡 Exam Tip: When evaluating a == b && c > d, relational operators (==, >) are evaluated BEFORE logical AND (&&). So the expression is treated as (a == b) && (c > d).
🔄 Strategy for Tracing Expression Output in Exams
1

Identify Parentheses First

Always solve innermost parentheses () first before outside operations.

2

Scan for Unary & Multiplicative Operators

Execute Unary (++, --, !) and then Multiplicative (*, /, %).

3

Apply Associativity for Tied Rank

If two operators share equal rank, apply evaluation direction (usually Left-to-Right).

4

Complete Assignment Last

Assign final calculated value to left-hand variable using = (Right-to-Left).

⚠️ Common Mistake: Modulo (%), Multiplication (*), and Division (/) share the **exact same precedence level**. They are evaluated strictly from **Left to Right** as they appear!
📝 Quick Revision — Key Exam Points
  • 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.

Post a Comment

Thanks for comment.

Previous Post Next Post