Lesson 3 — Beginner
Operators in C
Operators wo symbols hain jo values ke saath kaam karte hain — jodna, todna, compare karna, bit chedna. Is lesson mein hum har type ke operators dekhenge, unke real examples with output, aur wo traps jo interview mein poochhe jaate hain.
Arithmetic aur assignment operators
WHAT
Arithmetic operators +, -, *, /, % calculation ke liye hain. Assignment operators (=, +=, -=, *=, /=, %=) value ko variable mein daalte hain. C mein % (modulus) remainder deta hai.
WHEN
Har jagah — bill banana ho, average nikalna ho, even/odd check karna ho, counter badhana ho — sab operators se hi hota hai.
WHERE
Har program mein. Loop counter (i++), condition (a > b), calculation (total/n) — operators C ki nass-nass mein hain.
int a = 10, b = 3;
printf("%d\n", a + b); // 13
printf("%d\n", a - b); // 7
printf("%d\n", a * b); // 30
printf("%d\n", a / b); // 3 (integer division, fraction gaya)
printf("%d\n", a % b); // 1 (remainder)
a += 5; // a = a + 5 → 15
a -= 5; // a = a - 5 → 10
a *= 2; // a = a * 2 → 20
a /= 2; // a = a / 2 → 10
Relational aur logical operators
int x = 5, y = 10;
printf("%d\n", x == y); // 0 (false)
printf("%d\n", x != y); // 1 (true)
printf("%d\n", x < y); // 1
printf("%d\n", x >= 5); // 1
int p = 1, q = 0;
printf("%d\n", p && q); // 0 AND — dono chahiye
printf("%d\n", p || q); // 1 OR — koi ek kaafi
printf("%d\n", !p); // 0 NOT — ulta kar do
- C mein true = 1, false = 0. Har condition isi number mein convert hoti hai.
- == vs = — do bar equal = comparison, ek bar = assignment. Ye sabse famous trap hai.
- Chhote numbers: x > 5 && x < 10 likhna valid hai — Python wala "5 < x < 10" C mein galat ho jaayega.
- Range check: chahiye toh (x > 5) && (x < 10) — brackets se clear rakho.
Bitwise operators — asli taakat
5 & 3 = 1 (0101 & 0011 = 0001) AND
5 | 3 = 7 (0101 | 0011 = 0111) OR
5 ^ 3 = 6 (0101 ^ 0011 = 0110) XOR
~5 = ...11111010 NOT (2s complement)
5 << 1 = 10 (0101 << 1 = 1010) left shift — 2 se guna
5 >> 1 = 2 (0101 >> 1 = 0010) right shift — 2 se bhaag
// Even/Odd — ek instruction mein
if (n & 1) printf("Odd"); else printf("Even");
// Swap bina temp variable ke
a = a ^ b; b = a ^ b; a = a ^ b;
// Masking tricks
n & (1 << 3) // 3rd bit set hai ya nahi
n | (1 << 3) // 3rd bit set kar do
n & ~(1 << 3) // 3rd bit clear kar do
Increment/decrement aur ternary
int i = 5;
printf("%d ", i++); // 5 — pehle value lo, phir +1
printf("%d ", i); // 6 — ab badal chuka
printf("%d ", ++i); // 7 — pehle +1, phir value
printf("%d", i); // 7
// i ab 7 — output: 5 6 7 7
int a = 10, b = 3;
int max = (a > b) ? a : b; // 10 — condition ? sahi : galat
int absx = (a < 0) ? -a : a;
Ternary operator (cond ? x : y) chhoti if-else ki jagah hai. Lekin zyada lambi ternary padhna mushkil hota hai — wahan if-else hi likho.
Precedence aur associativity
Highest
1. () [] -> .
2. ++ -- ! ~ + - (unary)
3. * / %
4. + -
5. << >>
6. < <= > >=
7. == !=
8. &
9. ^
10. |
11. &&
12. ||
13. ?:
14. = += -= *= ...
15. , Lowest
Ratne ki zarurat nahi — brackets lagao aur confuse mat ho. a + b * c matlab a + (b * c) kyunki * pehle chalta hai. Doubt ho toh parentheses sabse upar hai.
- Same level ke operators pe associativity decide karti hai: arithmetic left-to-right, assignment right-to-left.
- a = b = c = 5 chalta hai kyunki = right-to-left chalta hai (pehle c=5, phir b, phir a).
- Bitwise aur logical ko mila mat karo — p && a == b ka matlab (p && (a == b)) hai, (p && a) == b nahi.
Exercise
Question: 5 % 3 ka result kya hai? Sirf number daalo.
Question: 5 << 1 (left shift) ka result kya hai? Sirf number daalo.
Question: int i = 5; printf("%d", i++); ka output kya hai? Sirf number daalo.
Question: Comparison ke liye kaunsa operator use hota hai — = ya ==?
Common mistakes
- = aur == ka confusion: if (x = 5) likhoge toh x mein 5 assign ho jaayega aur condition hamesha true (5) — bug chhup jaata hai. Comparison mein hamesha ==.
- Integer division ka trap: 10 / 3 = 3 (3.33 nahi). Aur 10 / 3 * 3 = 10 nahi, 9 — division pehle hota hai aur fraction pehle hi gayab.
- Post increment ki value turant man lena: i++ pehle purani value deta hai, increment baad mein. printf("%d %d", i++, i++) jaisi line likho hi mat.
Bitwise operators daily kaam aate hain — jaise rickshaw meter har 100m pe click hota hai, waise hi n & 1 se check hota hai number even hai ya odd:
#include <stdio.h>
int main() {
int n = 7;
printf("%d is %s\n", n, (n & 1) ? "ODD" : "EVEN");
printf("%d is %s\n", 8, (8 & 1) ? "ODD" : "EVEN");
return 0;
}
Output:
7 is ODD
8 is EVEN- TCS NQT: pre/post increment wale output questions — har baar aate hain.
- GATE: bitwise operators aur operator precedence deep questions.
- College exam: modulus se reverse number / even-odd program likhwana.
- Trap: "a % b jab a < b hota hai toh a" — sahi hai, ye property poochhi jaati hai.
Operators ready. Agla lesson — Input & Output — mein seekhenge printf aur scanf se program ko data kaise dete aur lete hain.