5.1.25 · D3 · HinglishC Programming

Worked examplesEnumerations

2,355 words11 min read↑ Read in English

5.1.25 · D3 · Coding › C Programming › Enumerations

Yeh page ek shooting range hai: hum har tarah ki enum situation list karte hain jo C aapke saamne rakh sakta hai, phir har ek ko fully worked example se knock down karte hain. Agar aapne abhi tak nahi dekha ki enums kaise count karte hain, toh pehle parent note padho: Enumerations.

Shuru karne se pehle, ek promise us parent note se jo hum baar baar use karenge:

Neeche sab kuch is hi rule ka application hai, lekin increasingly tricky inputs par.


Scenario matrix

Samjho ki har enum jo aap kabhi bhi milenge woh in cells mein se kisi ek mein land karti hai. Hamara kaam hai inhe saare hit karna.

# Case class Kya tricky hai Kaun cover karta hai
A Pure defaults seedha 0,1,2,… counting Ex 1
B Explicit start, phir resume +1 rule kisi bhi number se continue hoti hai Ex 2
C Negative explicit value counting abhi bhi +1 hai, zero se neeche se Ex 3
D Duplicate values do naam, ek integer — legal hai Ex 4
E Zero / empty-ish & sentinel trailing COUNT ek array ko size karta hai Ex 5
F Out-of-range assignment enum c = 99; — "no type safety" ka edge case Ex 6
G Real-world word problem enum vs magic numbers mein se choose karna Ex 7
H Exam twist — mixed = aur gaps poori chain haath se trace karo Ex 8

Neeche Cells A–H har ek label ki gayi hain. Saath milke yeh defaults, explicit, resumed, negative, duplicate, zero/sentinel, degenerate/out-of-range, real-world, aur exam-style cover karti hain — koi bhi scenario chhuta nahi.

Neeche figure woh mental model dikhati hai jo hum har jagah use karte hain: number line par named sticky-notes.

Figure — Enumerations

Worked examples

Sentinel trick ek picture ki haqdar hai — aakhri note ek tally hai, koi din nahi:

Figure — Enumerations

Active recall

Recall Har cell ke liye ek line — matrix yaad se cover karo
  • Cell A: enum {X,Y,Z}; mein Z ki value? → 2
  • Cell B: enum {P=200,Q,R}; mein R ki value? → 202
  • Cell C: enum {N=-3,M}; mein M ki value? → -2
  • Cell D: kya do enumerators dono 5 ke barabar ho sakte hain? → Haan
  • Cell E: enum{A,B,C,COUNT}; mein COUNT ki value? → 3
  • Cell F: kya enum Color c = 99; compile hota hai? → Haan, 99 print karta hai
  • Cell H: enum{J,K=4,L,M=J}; mein M ki value? → 0 (J=0)

Trace-the-chain drills:

enum {A, B=4, C}; mein C ki value
5
enum {N=-3, M}; mein M ki value
-2 (counting negatives se guzarti hai)
Kya enum X { A=5, B=5 }; legal hai?
Haan — sirf names unique hone chahiye, values repeat ho sakti hain
enum{RED,GREEN,BLUE,COUNT}; mein COUNT kya hai?
3 (automatically real items ki count ke barabar)
Kya enum Color c = 99; C mein compile hota hai?
Haan — enums int se freely convert hote hain, koi clamping nahi

Connections

  • Enumerations — parent note; yeh page uska worked-examples annex hai.
  • Integer Types in C — kyun sizeof(int) ne Ex 5 mein array-byte counts diye.
  • switch statement — Ex 7 ki state machine enum par switch hoti hai.
  • Arrays in C — sentinel-sizing trick (Ex 5, Ex 7).
  • struct in C — ek enum "tag" ko struct ke saath pair karo tagged unions ke liye.
  • typedeftypedef enum {...} State; Ex 7 mein enum keyword hata deta hai.

Scenario Map

starts at 0

resumes from n

walks through zero

same value allowed

last item is count

sizes

combine gaps

no type safety

Rule value = prev plus one

Cell A defaults

Cell B explicit resume

Cell C negative

Cell D duplicates

Cell E sentinel count

Cell F out of range

Cell G real world

Cell H exam twist