5.1.25 · D1C Programming

Foundations — Enumerations

3,390 words15 min readBack to topic

Before you can understand a single line of the parent note, you need to be fluent in the tiny alphabet of ideas it silently assumes. This page builds every one of them from nothing, in the order they depend on each other.


1. What is an integer (and why the computer loves it)?

The picture: think of a row of numbered lockers stretching left and right. Each locker is a whole step from the next — you can stand on locker 3, but never between locker 3 and locker 4.

Figure — Enumerations

Why the topic needs it: every enum name is secretly one of these lockers. When the parent note says "an enum is just ints", it means each name is glued to exactly one whole-number locker. If you're shaky on what an integer is, see Integer Types in C.


2. What is a constant?

The picture: a locker whose door is welded shut at locker 5. You can look at it, point at it, use its number — but you can never make that name mean a different locker later.

Why the topic needs it: every enumerator (defined in the next section) is a constant. Once a name means 0, it means 0 forever. This is different from a variable, which is a locker whose contents you can swap.


3. What is an enumerator?

The picture: the enum is a row of jars; each jar with its name-label is one enumerator. RED is jar-0's label, GREEN is jar-1's label, and so on.

Why the topic needs it: the whole parent note is about enumerators — the recurrence in Section 7, the value(...) mapping, the switch cases and the array-index trick all operate on enumerators. You cannot follow a single formula until you know that the symbol stands for "the -th enumerator". We define formally once we reach the subscript in Section 6.


4. The enum keyword and what an enumeration actually is

We have used the word "enum" repeatedly — now we pin it down as a real piece of the C language.

The picture: the enumeration is the whole labelled shelf; each jar on it is an enumerator. enum is the sign hung above the shelf announcing "this is a shelf of named numbers".

The anatomy of the declaration, piece by piece:

  • enum — the keyword that starts every enumeration.
  • Color — the tag, an optional name for this new type so you can later declare enum Color c;.
  • { ... } — the braces holding the comma-separated enumerators.

Why the topic needs it: the parent's every example opens with enum. Until you know it's a keyword (not a name you invented) and that "enumeration" means the whole group while "enumerator" means one member, the sentence "an enum declares a new integer type whose values are a list of enumerators" is a tangle. To later drop the enum word from enum Color c; and just write Color c;, see typedef.


5. The = sign in C — assignment vs. definition

The picture: an arrow pointing from the number to the name — the number flows in and sticks.

Why the topic needs it: the parent's "explicit values" (OK = 1) rely on this. Miss the difference and WARN = 5 looks like ordinary assignment when it is really a one-time welding of a name to a number.


6. What is a type?

The picture: lockers come in different sizes and shapes. A char locker is small; an int locker is bigger; an enum Color locker is the same size as an int but wears a sticky label saying "colours live here".

Figure — Enumerations

Why the topic needs it: the parent says "an enum declares a new integer type". Without knowing what a type is, that sentence is empty. And it explains the third mistake in the parent: enum Color is a label on an int-sized box, not a locked vault — you can still shove 99 in it.


7. Subscript notation , the value(...) mapping, and the recurrence

The parent writes:

Three pieces of notation here are new. Let's earn them one at a time.

The picture: a shelf of named jars in a line. is the leftmost jar, the next, and so on. The subscript is the label under each jar telling you which slot it's in — not what's inside it.

The picture: is reading the number off the front of the jar. Point at a jar (an enumerator), and tells you its locker number.

The picture: dominoes. Once the first jar's value is fixed at 0, each following jar reads its left neighbour's number and adds 1. Knock the first, they all fall into place.

Figure — Enumerations

Why "+1 and not ×2 or +2"? Because that is exactly the rule the C standard chose: it wanted the simplest, most predictable count — 0, 1, 2, 3 — so array indices and enum values line up perfectly. Any other step would break the array-sizing trick from the parent's example 4.


8. Function calls and printf("%d", x)

Before we can read the parent's printf(...) lines, we need the shape of a function call itself — the parent assumes it, so we build it here.

The picture: a vending machine. The name is the machine; the parentheses are the coin-slot; each argument is a coin you drop in. Push the button and out comes the result (here, printed text).

The picture: %d is a stencil shaped like a number. Whatever you pour through it comes out as digits — never as letters or names.

Why the topic needs it: the parent's first big mistake — "printing an enum gives its name" — dies here. printf("%d", RED) pours the number 0 through the number-stencil, so you see 0, never "RED". There is no name inside the machine to print.


9. Arrays, indexing syntax, and the COUNT trick

The picture: an egg carton. Box 0, box 1, box 2 ... you point at one by writing its slot number inside [ ].

Why the topic needs it: the parent's example int days[MONTH_COUNT]; uses that enumerator's value () as the size inside the array-creation brackets, and enum names as indices into it. Enum counting-from-0 and array counting-from-0 are deliberately the same, which is what makes the MONTH_COUNT sentinel trick work. See Arrays in C.


How these feed the topic

How to read the map below: each arrow means "the thing at the tail must be understood before the thing at the head." Follow the arrows and you are literally walking the safe learning order — start at the boxes with no incoming arrows (Integer), and every box you reach will only use ideas you have already passed through. The node Enumeration enum is the destination: notice how every foundation on this page eventually feeds into it, which is why you had to build them first.

Integer whole number

Constant welded value

Enumerator one named constant

enum keyword

Equals sign as definition

Type labelled box

Subscript index e_k

value mapping enumerator to int

Recurrence prev plus one

Function call name and parentheses

printf and percent d

Array and index brackets

Enumeration enum

Each box on the left is something this page built from zero; together they hold up the parent idea of an enumeration. Related type-building tools you'll meet nearby: struct in C, typedef, and the place enums shine most — the switch statement.


Equipment checklist

Give me a whole number with no fractional part — is 3.5 one?
No — an integer has no decimal part; 3 and 4 are, 3.5 is not.
What is the difference between a constant and a variable?
A constant is welded and never changes; a variable is a box you can refill.
What exactly is an enumerator?
One named constant listed inside an enum — a single name welded to a single integer.
What is the difference between an enumeration and an enumerator?
The enumeration is the whole labelled group (the shelf); an enumerator is one member (one jar).
Is enum a keyword or a name you invent?
A reserved keyword — you cannot use it as an ordinary variable name.
Inside an enum, what does RED = 0 mean?
It permanently defines the enumerator RED to stand for the value 0 (a definition, not a normal assignment).
What does the word "type" tell the compiler?
What kind of value a box holds and how big the box is.
Is the underlying integer type of an enum guaranteed to be int?
No — it is implementation-defined (the compiler picks an integer type big enough), so range and overflow behaviour are too.
In , what does the subscript represent?
The position/index of the enumerator, counting from 0.
What does the mapping hand back?
The integer that the -th enumerator stands for.
State the recurrence for default enum values in words.
Each enumerator's value = the previous enumerator's value + 1.
In enum M { A=1, B, C=5, D };, what are the four values?
1, 2, 5, 6 — an explicit value resets the count wherever it appears.
If an enum starts at A = -2, what are the next two values?
-1 and 0 (the +1 rule ignores sign).
Can two enumerators share the same value, as in A=1, B=1?
Yes — only the names must be unique, not the values.
What can go wrong with a huge explicit value like = 5000000000?
It may exceed the implementation-defined underlying type's range and be non-portable or overflow.
What are the parentheses in printf("%d", x) for?
They hold the arguments handed to the function call.
What do the square brackets in int days[3]; and days[2] do?
Creating: set the number of boxes; indexing: pick one box by position (from 0).
Why does a trailing MONTH_COUNT enumerator equal the item count?
The +1 recurrence lands it one past the last real item, which equals how many came before it.
Why do enums and arrays both start counting at 0?
So enum values line up as valid array indices, making the COUNT-sentinel sizing trick work.