KYA milta hai: readability (RED nahi 0), related constants ki grouping, aur thoda sa type-checking documentation.
HOW under the hood: ek enum bas ints hi hai. Names compile-time labels hain; runtime par kuch nahi hota sirf integers ke.
Yeh rule kyun hai (first principles se derived): C standard kehta hai ki pehle enumerator ko bina initializer ke 0 milta hai. Har agla enumerator bina initializer ke pichle enumerator ki value se ek zyada hota hai. Woh recurrence value(ek)=value(ek−1)+1 poora rule hai — baaki sab usi se follow hota hai.
C mein enumeration declare karne wala keyword kaunsa hai?
enum
Default mein, pehle enumerator ko kaun si value milti hai?
0
Har un-initialized enumerator ki value kaise compute hoti hai?
previous enumerator ki value + 1
enum X { A=10, B, C }; mein C kya hai?
12
C mein ek enum constant ki underlying storage type kya hai?
int (ek integer)
Kya do enumerators same integer value share kar sakte hain?
Haan; sirf names unique honi chahiye
Kya printf("%d", RED) "RED" naam print karta hai?
Nahi — yeh integer print karta hai; C mein koi name lookup nahi hai
Enumerator ko kaise reference karte hain — Color.RED ya RED?
Bas RED (enumerators surrounding scope mein rehte hain)
COUNT jaisa final enumerator use karne ka common trick kya hai?
Yeh automatically preceding items ki count ke barabar hota hai, array sizes ke liye useful hai
Kya enum Color c = 99; ek compile error hai?
Nahi — C mein enums int se/ko freely convert hote hain
Recall Feynman: ek 12-saal ke bachche ko explain karo
Socho numbered lockers: locker 0, locker 1, locker 2. Numbers machine ke liye easy hain par tumhare liye boring. Toh tum har ek par ek sticky-note naam chipka do: locker 0 par "RED", locker 1 par "GREEN". Ab tum kehte ho "open RED" aur machine phir bhi locker 0 kholti hai — sticky note bas tumhare liye hai. Ek enum wahi sticky notes ka set hai. Lockers abhi bhi numbers hain; names wahan hain taaki humans code padh sakein. Aur agar tum numbers nahi chunte, C bas count karta rehta hai: 0, 1, 2, 3...