5.1.25 · D4 · HinglishC Programming

ExercisesEnumerations

2,063 words9 min read↑ Read in English

5.1.25 · D4 · Coding › C Programming › Enumerations

Woh ek rule jo neeche lagbhag har problem ko power deta hai:

Figure — Enumerations

L1 — Recognition

Exercise 1

Har enumerator ki integer value batao:

enum Dir { NORTH, EAST, SOUTH, WEST };
Recall Solution

Kisi bhi enumerator ki explicit value nahi hai, isliye hum 0 se shuru karke har step mein 1 add karte hain (recurrence with ).

  • NORTH = 0 (pehla, koi init nahi → 0)
  • EAST = 0 + 1 = 1
  • SOUTH = 1 + 1 = 2
  • WEST = 2 + 1 = 3

Exercise 2

Sach ya jhooth, aur kyun: enum Color { RED, GREEN, BLUE }; mein printf("%d", GREEN); statement text GREEN print karta hai.

Recall Solution

Jhooth. C ek enumerator ko plain int ke roop mein store karta hai. Runtime par koi name table nahi hoti — identifier GREEN ek compile-time label tha jise compiler pehle hi number 1 se replace kar chuka hai. Isliye %d 1 print karta hai. Word print karne ke liye aapko apna array chahiye hoga: const char* names[] = {"RED","GREEN","BLUE"}; aur names[GREEN] print karo.


L2 — Application

Exercise 3

Har enumerator ki value do:

enum E { A = 4, B, C, D = 10, E2, F };
Recall Solution

"Explicit overwrites, phir +1 continue karta hai" apply karo:

  • A = 4 (explicit)
  • B = 4 + 1 = 5
  • C = 5 + 1 = 6
  • D = 10 (explicit — count restart karta hai)
  • E2 = 10 + 1 = 11
  • F = 11 + 1 = 12

Exercise 4

Yeh kya print karta hai, aur kyun?

enum Http { OK = 200, CREATED, ACCEPTED, MOVED = 301, FOUND };
printf("%d %d\n", ACCEPTED, FOUND);
Recall Solution
  • OK = 200 (explicit)
  • CREATED = 201, ACCEPTED = 202
  • MOVED = 301 (explicit restart)
  • FOUND = 302

Output: 202 302.

Exercise 5

Blanks fill karo taaki LOW, MID, HIGH 10, 20, 30 ke barabar ho jayein:

enum Level { LOW = ___, MID = ___, HIGH = ___ };
Recall Solution

Yahan +1 rule se 10 ke jumps nahi milenge, isliye hume teeno explicitly set karne honge:

enum Level { LOW = 10, MID = 20, HIGH = 30 };

(Tum likh sakte ho LOW = 10, MID = LOW + 10, HIGH = MID + 10 — constants par arithmetic allowed hai kyunki har ek compile-time integer hai.)


L3 — Analysis

Exercise 6

Kya ye dono declarations ek hi file mein legal hain? Explain karo.

enum A { CAT, DOG };
enum B { DOG, FISH };
Recall Solution

Illegal. Enumerators apne enum ke andar scoped nahi hote — woh surrounding scope mein plain identifiers ki tarah dal diye jaate hain (unlike ek struct field, jiske liye s.field chahiye). enum A aur enum B dono ek hi scope mein identifier DOG define karne ki koshish karte hain, jo ek redefinition error hai. Sirf names unique hone chahiye; jis enum se woh belong karte hain woh namespace nahi banata.

Exercise 7

Kya yeh legal hai? Values kya hain?

enum Sig { WARN = 5, ERR, FATAL = 5 };
Recall Solution

Legal. Alag names ek hi integer value share kar sakte hain.

  • WARN = 5 (explicit)
  • ERR = 5 + 1 = 6
  • FATAL = 5 (explicit — WARN ki duplicate value, lekin alag naam)

case WARN: aur case FATAL: ke saath switch (s) ek compile error hoga — dono labels 5 ke barabar hain, aur switch statement ek hi value ke do case labels forbid karta hai.

Exercise 8

enum Color { RED, GREEN, BLUE }; enum Color c = 99; — kya yeh compile hoga? Kya store hoga?

Recall Solution

Compile hota hai. C mein ek enum int se freely convertible hai, isliye 99 assign karna allowed hai bhale hi 99 koi color name nahi karta. c ab integer 99 hold karta hai. C yahan essentially koi type safety nahi deta — ek enum ko ek documented int ki tarah treat karo. (Agar c par switch ho to woh simply RED/GREEN/BLUE mein se kisi ko match nahi karega aur default par fall through ho jaayega.)


L4 — Synthesis

Exercise 9

"Trailing COUNT" trick use karke ek array declare karo jisme har weekday ke liye exactly ek slot ho, bina 7 hard-code kiye. Enum aur array line dikhao.

Recall Solution

End mein ek sentinel rakho taaki woh automatically usse pehle items ki count ke barabar ho jaye (0..6 → count 7):

enum { MON, TUE, WED, THU, FRI, SAT, SUN, DAY_COUNT };
int hours[DAY_COUNT];   // DAY_COUNT == 7

MON=0 … SUN=6, isliye DAY_COUNT = 6 + 1 = ==7==. Sentinel se pehle ek naya din add karo aur DAY_COUNT apne aap update ho jaata hai — array bina number touch kiye resize ho jaata hai. Dekho Arrays in C.

Exercise 10

Ek name table ke saath enum combine karo taaki printf sach mein word dikha sake. enum Color { RED, GREEN, BLUE } ke liye ek const char* array aur print line likho, phir batao printf("%s\n", names[GREEN]); kya print karta hai.

Recall Solution
enum Color { RED, GREEN, BLUE };
const char* names[] = { "RED", "GREEN", "BLUE" };
printf("%s\n", names[GREEN]);   // GREEN

Kyun kaam karta hai: GREEN integer 1 hai, isliye names[GREEN] names[1] hai, jo string "GREEN" hai. Array woh name lookup hai jo C ne kabhi nahi di — tum use khud provide karte ho. Output: GREEN. (Yeh tab tak safe hai jab tak har enumerator ek valid index 0..2 hai, ek aur wajah ki default 0-based numbering itni convenient hai.)


L5 — Mastery

Exercise 11

States LOCKED, UNLOCKED aur events PUSH, COIN ke saath ek turnstile ke liye state machine design karo. Do enums aur ek switch use karke transition rule likho: ek COIN unlock karta hai; ek unlocked turnstile par PUSH use wapas lock karta hai. Phir LOCKED se shuru karke event sequence PUSH, COIN, PUSH, PUSH ke baad state trace karo.

Recall Solution
enum State { LOCKED, UNLOCKED };      // 0, 1
enum Event { PUSH, COIN };            // 0, 1
 
enum State next(enum State s, enum Event e) {
    switch (s) {
        case LOCKED:
            return (e == COIN) ? UNLOCKED : LOCKED;
        case UNLOCKED:
            return (e == PUSH) ? LOCKED : UNLOCKED;
    }
    return s;  // unreachable, silences warnings
}

LOCKED se trace:

  1. LOCKED par PUSHCOIN nahi → LOCKED rehta hai
  2. LOCKED par COINCOINUNLOCKED
  3. UNLOCKED par PUSHPUSHLOCKED
  4. LOCKED par PUSHCOIN nahi → LOCKED rehta hai

Final state: LOCKED (integer value 0). Enums ki wajah se har case English jaisa padhta hai — enums ko switch statement ke saath pair karne ka classic payoff yehi hai.

Figure — Enumerations

Exercise 12

Color ko enum keyword ke bina usable banane ke liye typedef use karo, phir ek variable declare karo aur BLUE assign karo. Woh kaunsa integer hold karta hai?

Recall Solution
typedef enum { RED, GREEN, BLUE } Color;
Color c = BLUE;
printf("%d\n", c);   // 2

typedef anonymous enum ko alias Color deta hai, isliye tum enum Color c ki jagah Color c likhte ho. Kyunki RED=0, GREEN=1, BLUE=2 hai, variable integer 2 hold karta hai. Output: 2.


Active recall

Recall Rapid re-test (attempt karne ke baad kholo)

{NORTH,EAST,SOUTH,WEST} mein WEST ki value? ::: 3 enum E { A=4, B, C, D=10, E2, F }; mein F ki value? ::: 12 {OK=200,CREATED,ACCEPTED,MOVED=301,FOUND} mein FOUND ki value? ::: 302 Kya WARN=5 aur FATAL=5 saath exist kar sakte hain? ::: Haan — names alag hain, values repeat ho sakti hain {MON..SUN, DAY_COUNT} mein DAY_COUNT? ::: 7 LOCKED se PUSH,COIN,PUSH,PUSH ke baad turnstile ka final state? ::: LOCKED (0) Color c = BLUE; mein kaunsa integer hold hota hai? ::: 2

Connections

  • Parent: Enumerations in C
  • Integer Types in C — kyun har enumerator asal mein ek int hai.
  • switch statement — Exercises 7, 11.
  • Arrays in CCOUNT sizing trick (Ex. 9, 10).
  • struct in C — contrast: struct fields scoped hote hain, enumerators nahi (Ex. 6).
  • typedef — Exercise 12.