5.1.25 · HinglishC Programming

Enumerations

1,531 words7 min readRead in English

5.1.25 · Coding › C Programming


Enums KYU exist karte hain?

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.


Definition

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 poora rule hai — baaki sab usi se follow hota hai.

Figure — Enumerations

Enums ka use — worked examples


Common mistakes (Steel-manned)


Active recall

Recall Khud test karo (try karne ke baad kholo)
  • Pehle un-initialized enumerator ko kaun sa integer milta hai? → 0
  • enum E { A=4, B, C };C ki value? → 6 (B=5, C=6)
  • Kya do enumerators ki value 7 ho sakti hai? → Haan, sirf names alag hoti hain.
  • {RED,GREEN,BLUE} ke liye printf("%d", GREEN) kya print karta hai? → 1
  • Enum constants ka underlying type kya hai? → int

Flashcards

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...


Connections

  • C Constants and #define — named values ke liye enums vs #define (enums scoped & typed hain, #define raw text hai).
  • Integer Types in C — enums int ke roop mein store hote hain.
  • switch statement — sabse common jagah jahan enums shine karte hain.
  • struct in C — dono naye types define karte hain; enums values ko name dete hain, structs data ko group karte hain.
  • Arrays in C — arrays ko size karne ke liye trailing COUNT enumerator ka use.
  • typedeftypedef enum {...} Color; taaki enum keyword drop ho sake.

Concept Map

solved by

declares

stored as

gives

numbered by

derived from

then resumes via

can set

allows

matched in

Magic numbers

Enumeration enum

Enumeration constants

Underlying int type

Default numbering rule

Recurrence value = prev + 1

Explicit values

Duplicate values legal

Readable code

Use in switch