Operators
Arithmetic operators (+ - * / %) numbers par maths karte hain. Integer division mein decimal part chhoot jaata hai — 7 / 2 result deta hai 3, na ki 3.5 (agar dono operands int hain).
Relational operators (== != > < >= <=) comparison karte hain, result 0 (false) ya 1 (true) hota hai (C mein purane zamane mein true/false keywords nahi the). Logical operators (&& || !) conditions jodte hain.
Increment/decrement operators (++ --) bahut common hain C mein — i++ (post-increment) aur ++i (pre-increment) mein farq interview mein bahut pucha jaata hai.
int a = 10, b = 3;
printf("%d\n", a % b); // 1
printf("%d\n", a / b); // 3 (integer division!)
printf("%d\n", a > b && b > 0); // 1 (true)- Integer division decimal chhod deta hai (7/2 = 3)
- true/false ki jagah 0/1 use hota hai
- i++ vs ++i ka farq interview favorite hai
C mein bitwise operators bahut common hain (especially embedded/systems programming mein): & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), >> (right shift) — ye directly binary bits par kaam karte hain.
Left shift (<<) se multiply by 2 ka fast tareeka milta hai, right shift (>>) se divide by 2 — x << 1 same hai x * 2 ke, aur processors ke liye shift operation, multiplication se fast hoti hai.
int a = 5; // binary: 0101
int b = 3; // binary: 0011
printf("%d\n", a & b); // 1 (0001)
printf("%d\n", a | b); // 7 (0111)
printf("%d\n", a ^ b); // 6 (0110)
printf("%d\n", a << 1); // 10 (1010, same as a*2)+=, -=, *=, /=, %= — ye shortcuts hain jo "variable = variable operator value" ko chhota kar dete hain. x += 5 same hai x = x + 5 ke, bas likhne mein chhota aur padhne mein clear.
int x = 10;
x += 5; // x = 15
x -= 3; // x = 12
x *= 2; // x = 24
x /= 4; // x = 6Jab ek expression mein multiple operators ho, order matter karta hai — * / % pehle chalte hain + - se pehle, jaise maths mein. Confusion se bachne ke liye parentheses () use karo, chahe technically zaroorat na ho.
int result = 2 + 3 * 4; // 14, na ki 20 (multiplication pehle)
int clear = 2 + (3 * 4); // same result, lekin readable