5.1.25C Programming

Enumerations

1,719 words8 min readdifficulty · medium

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 value(ek)=value(ek1)+1value(e_k)=value(e_{k-1})+1 is the whole rule — everything else follows from it.

Figure — Enumerations

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 of C? → 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?
enum
By default, what value does the first enumerator get?
0
How is each un-initialized enumerator's value computed?
previous enumerator's value + 1
In enum X { A=10, B, C };, what is C?
12
What is the underlying storage type of an enum constant in C?
int (an integer)
Can two enumerators share the same integer value?
Yes; only the names must be unique
Does printf("%d", RED) print the name "RED"?
No — it prints the integer; C has no name lookup
How do you reference an enumerator — Color.RED or RED?
Just RED (enumerators live in the surrounding scope)
What is a common trick using a final enumerator like COUNT?
It auto-equals the number of preceding items, useful for array sizes
Is enum Color c = 99; a compile error?
No — enums freely convert to/from int in C

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 #define for named values (enums are scoped & typed, #define is 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 COUNT enumerator to size arrays.
  • typedeftypedef enum {...} Color; to drop the enum keyword.

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

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.

Go deeper — visual, from zero

Test yourself — C Programming

Connections