Enumerations
WHY do enums exist?
WHAT you get: readability (RED not 0), grouping of related constants, and a tiny bit of type-checking documentation.
HOW under the hood: an enum is just ints. The names are compile-time labels; at runtime there is nothing but integers.
The definition
Why this rule (derived from first principles): The C standard says the first enumerator without an initializer is 0. Each subsequent enumerator without an initializer is one greater than the previous enumerator's value. That recurrence is the whole rule — everything else follows from it.

Using enums — worked examples
Common mistakes (Steel-manned)
Active recall
Recall Test yourself (open after attempting)
- What integer does the first un-initialized enumerator get? → 0
enum E { A=4, B, C };— value ofC? → 6 (B=5, C=6)- Can two enumerators have value 7? → Yes, names just differ.
- What does
printf("%d", GREEN)print for{RED,GREEN,BLUE}? → 1 - What is the underlying type of enum constants? → int
Flashcards
What keyword declares an enumeration in C?
enumBy default, what value does the first enumerator get?
How is each un-initialized enumerator's value computed?
In enum X { A=10, B, C };, what is C?
What is the underlying storage type of an enum constant in C?
Can two enumerators share the same integer value?
Does printf("%d", RED) print the name "RED"?
How do you reference an enumerator — Color.RED or RED?
RED (enumerators live in the surrounding scope)What is a common trick using a final enumerator like COUNT?
Is enum Color c = 99; a compile error?
Recall Feynman: explain to a 12-year-old
Imagine numbered lockers: locker 0, locker 1, locker 2. Numbers are easy for the machine but boring for you. So you tape a sticky-note name on each: "RED" on locker 0, "GREEN" on locker 1. Now you say "open RED" and the machine still opens locker 0 — the sticky note is just for you. An enum is that set of sticky notes. The lockers are still numbers; the names are there so humans can read the code. And if you don't pick numbers, C just counts up: 0, 1, 2, 3...
Connections
- C Constants and #define — enums vs
#definefor named values (enums are scoped & typed,#defineis raw text). - Integer Types in C — enums are stored as
int. - switch statement — the most common place enums shine.
- struct in C — both define new types; enums name values, structs group data.
- Arrays in C — using a trailing
COUNTenumerator to size arrays. - typedef —
typedef enum {...} Color;to drop theenumkeyword.
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, enum ka matlab bahut simple hai: tum kuch related integer constants ko naam de dete ho taaki code padhne mein English jaisa lage, na ki random numbers ka dhera. Jaise enum Color { RED, GREEN, BLUE }; — yahan andar andar RED ka matlab 0 hai, GREEN ka 1, BLUE ka 2. Rule yaad rakho: pehla bina value wala enumerator hamesha 0 se start hota hai, aur har agla ek +1. Agar tum khud value de do (OK = 200), to aage wala uss se +1 le leta hai (CREATED = 201). Bilkul counter ki tarah.
Yeh important kyun hai? Kyunki bade programs mein "magic numbers" (jaise if (state == 2)) samajhna impossible ho jaata hai. enum se code self-documenting ban jaata hai aur switch ke andar case RED: likhna padhne mein clear lagta hai. Plus, ek chhota trick: list ke end mein COUNT daal do — wo automatically items ki ginti ban jaata hai, array size ke liye perfect.
Ek baat jo students galat samajhte hain: C mein enum ka naam print nahi hota! printf("%d", RED) likhoge to 0 chhapega, "RED" nahi. C ke andar enum sirf int hota hai — naam to bas tumhare (programmer ke) liye sticky-note hai. Aur do enumerators ki same value ho sakti hai, sirf naam unique hone chahiye. Itna yaad rakh lo to enum master ho gaya.