5.1.4C Programming

Operators — arithmetic, relational, logical, bitwise, assignment, comma

2,381 words11 min readdifficulty · medium1 backlinks

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

Figure — Operators — arithmetic, relational, logical, bitwise, assignment, comma

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?
An 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?
No, it's an argument separator. Use f((a, b)) to pass a comma expression.
Why does 5 / 2 give 2 but 5 / 2.0 gives 2.5?
Both 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?
Sets all to 0; = is right-associative and each = returns its assigned value.

Connections

Concept Map

acts on

count of operands

grouped by

each has

int / does

% gives

linked by

derived from

type decides

bug source

returns

confused with =

Operator - tiny machine

Operands values

Arity unary/binary/ternary

Operator groups

Return type + precedence

Arithmetic + - * / %

Relational + equality

Truncating division toward zero

Remainder sign of dividend

Identity a = a/b*b + a%b

int vs double promotion

5/2 = 2 not 2.5

int 1 true / 0 false

if x=5 assigns not compares

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.

Go deeper — visual, from zero

Test yourself — C Programming

Connections