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.

⏱ 13 min• Beginner• Variables & Data Types

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
Muh jawab: a % 2 == 0 → even, a % 2 == 1 → odd. Aur a % 10 = last digit, a / 10 = last digit hata kar. Ye teen line har beginner interview mein aati hain.

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
Short-circuit: p && q mein p false ho toh q kabhi check hi nahi hota. Isliye aksar likhte hain: (p != NULL && p->val == 5) — pehle NULL check, phir dereference. Bachaa hua crash!

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
Interview gold: XOR swap aur bit check (n & 1) interview mein bahut poochhte hain. Shift = fast multiply/divide by 2 — hardware engineers roz use karte hain.

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.

Trap: printf("%d %d", i++, i++) jaisi lines mein output UNDEFINED hai — evaluation order fix nahi. Aise questions exam mein aayein toh compiler ke soch ke do, warna ek-ek karke likha karo.

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.

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

Desi example: Auto-rickshaw meter ki trick

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:

c
#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
Exam mapping (TCS / GATE / College)
  • 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.
Lesson complete?

Operators ready. Agla lesson — Input & Output — mein seekhenge printf aur scanf se program ko data kaise dete aur lete hain.

Popular courses: Data Science Course in Lucknow · AI Course in Lucknow · Python Course in Lucknow · Power BI Course · Data Analytics Course