5.1.25 · D3C Programming

Worked examples — Enumerations

2,171 words10 min readBack to topic

This page is a shooting range: we list every kind of enum situation C can throw at you, then knock each one down with a fully worked example. If you have not yet seen how enums count, read the parent first: Enumerations.

Before we start, one promise from that parent note we will lean on constantly:

Everything below is this rule applied to trickier and trickier inputs.


The scenario matrix

Think of every enum you will ever meet as landing in one of these cells. Our job is to hit all of them.

# Case class What makes it tricky Hit by
A Pure defaults plain 0,1,2,… counting Ex 1
B Explicit start, then resume +1 rule continues from any number Ex 2
C Negative explicit value counting still means +1, from below zero Ex 3
D Duplicate values two names, one integer — legal Ex 4
E Zero / empty-ish & sentinel trailing COUNT sizing an array Ex 5
F Out-of-range assignment enum c = 99; — the "no type safety" edge Ex 6
G Real-world word problem choosing enum vs magic numbers Ex 7
H Exam twist — mixed = and gaps trace the whole chain by hand Ex 8

Cells A–H below are each labelled. Together they cover defaults, explicit, resumed, negative, duplicate, zero/sentinel, degenerate/out-of-range, real-world, and exam-style — no scenario left uncovered.

The figure below shows the mental model we use everywhere: named sticky-notes on a number line.

Figure — Enumerations

The worked examples

The sentinel trick is worth a picture — the last note is a tally, not a day:

Figure — Enumerations

Active recall

Recall One line per cell — cover the matrix from memory
  • Cell A: enum {X,Y,Z}; value of Z? → 2
  • Cell B: enum {P=200,Q,R}; value of R? → 202
  • Cell C: enum {N=-3,M}; value of M? → -2
  • Cell D: can two enumerators both equal 5? → Yes
  • Cell E: enum{A,B,C,COUNT}; value of COUNT? → 3
  • Cell F: does enum Color c = 99; compile? → Yes, prints 99
  • Cell H: enum{J,K=4,L,M=J}; value of M? → 0 (J=0)

Trace-the-chain drills:

Value of C in enum {A, B=4, C};
5
Value of M in enum {N=-3, M};
-2 (counting passes through negatives)
Is enum X { A=5, B=5 }; legal?
Yes — only names must be unique, values may repeat
In enum{RED,GREEN,BLUE,COUNT}; what is COUNT?
3 (auto-equals the number of real items)
Does enum Color c = 99; compile in C?
Yes — enums freely convert to/from int, no clamping

Connections

  • Enumerations — the parent; this page is its worked-examples annex.
  • Integer Types in C — why sizeof(int) gave us array-byte counts in Ex 5.
  • switch statement — the state machine of Ex 7 is switched on the enum.
  • Arrays in C — the sentinel-sizing trick (Ex 5, Ex 7).
  • struct in C — pair an enum "tag" with a struct for tagged unions.
  • typedeftypedef enum {...} State; drops the enum keyword in Ex 7.

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