Operators — arithmetic, relational, logical, bitwise, assignment, comma
WHY do we even classify operators?
We group operators because each group has its own return type and precedence rules. Mixing groups blindly (e.g. a & b == c) is the #1 source of bugs.
1. Arithmetic operators
2. Relational & equality operators
3. Logical operators
4. Bitwise operators
5. Assignment operators

6. The comma operator
Precedence cheat-table (high → low, the 80/20)
Recall Feynman: explain to a 12-year-old
Operators are like little kitchen tools. + is a mixer that adds, % is the tool that gives you the leftover slices after sharing pizza equally. && is a strict bouncer: BOTH conditions must be true to enter, and if the first person fails, he doesn't even check the second. Bit operators look at numbers as rows of light switches and flip them. And there's a "seating order" (precedence) — some tools always go first; if you don't like the order, wrap it in brackets to force your own.
Flashcards
What does the integer expression -7 % 2 evaluate to in C99, and why?
-1; % takes the sign of the dividend because / truncates toward zero: -7 - (-3)*2 = -1.What does a relational expression like (3 < 5) return as a value/type?
int equal to 1 (true) or 0 (false).Why is if (x = 5) always true?
= is assignment; it stores 5 in x and the expression's value is 5 (nonzero), so the condition is true. Use ==.What is short-circuit evaluation of && and ||?
&& stops if the left side is false; || stops if the left side is true — the right operand isn't evaluated.What does 5 << 2 equal and why?
20, because left shift by n multiplies by 2^n (5 × 4).Compute 12 & 10 bit by bit.
0000 1100 & 0000 1010 = 0000 1000 = 8.Why does if (a & b == c) misbehave?
== has higher precedence than &, so it parses as a & (b == c). Fix with (a & b) == c.What does x = (1, 2, 3); set x to, and why?
3; the comma operator evaluates each operand left to right and yields the last one.Is the comma in f(a, b) the comma operator?
f((a, b)) to pass a comma expression.Why does 5 / 2 give 2 but 5 / 2.0 gives 2.5?
int → integer (truncating) division; making one operand double promotes to real division.What's the division–remainder identity C guarantees?
a == (a/b)*b + (a%b).What does a = b = c = 0; do and why?
= is right-associative and each = returns its assigned value.Connections
- Data Types and Type Promotion — why
5/2≠5/2.0. - Sequence Points and Undefined Behavior — comma operator &
i = i++. - Two's Complement Representation — why
~,>>on signed ints behave as they do. - Control Flow — if and loops — relational/logical operators feed conditions.
- Pointers —
&&-guardingp != NULL && p->x. - Bit Masking Techniques — practical use of
& | ^ << >>.
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, C me har operator ek chhota machine hai: input do operands, output milta hai ek result. Sabse important baat — har operator ka return type aur precedence yaad rakho. Arithmetic me twist yeh hai ki / agar dono operands int ho to integer division karta hai, yaani 5/2 ka answer 2 aata hai, 2.5 nahi. Agar real division chahiye to ek operand ko double banao: 5/2.0. Aur % (remainder) ka sign hamesha dividend ke sign jaisa hota hai, isliye -7 % 2 = -1.
Relational aur logical operators ka result sirf 1 (true) ya 0 (false) hota hai. Yahan classic galti hai if (x = 5) likh dena — yeh == nahi, assignment hai, x me 5 set ho jaata hai aur condition hamesha true. Logical && aur || me short-circuit ka jadoo hai: && me agar left false hai to right check hi nahi hota, isliye p != NULL && p->val > 0 likh kar crash se bach sakte ho.
Bitwise operators (& | ^ ~ << >>) numbers ko bits ki line jaise dekhte hain. Yaad rakho: x << n ka matlab x * 2^n, aur x >> n ka matlab x / 2^n. Bahut bada trap: bitwise & ki precedence == se kam hai, isliye a & b == c actually a & (b == c) ban jaata hai — bracket lagana mat bhoolna.
Compound assignment x += y short form hai x = x + y ka, aur comma operator a, b dono ko evaluate karta hai par value sirf last wali (b) deta hai — for loop me do updates karne ke kaam aata hai. Mool mantra: jab confusion ho, parentheses lagao — 80% operator bugs wahin khatam.