Enumerations: named choices
Name states clearly while separating enum types, numeric representations, accepted inputs, and C23 extensions.
In this chapter8 sections
Named choices make code easier to read than unexplained numbers. An enumeration supplies names and values, but it does not validate every integer coming from outside the program. We will separate useful names, type rules, and valid application states.
1. Give names to choices
An enumeration defines an enumerated type and named enumeration constants. In this C17 example, the first implicit value is zero, an explicit value starts a new point in the sequence, and the next implicit value adds one. The resulting values must satisfy C17’s representability requirements.
enum State {
STATE_READY,
STATE_RUNNING = 4,
STATE_DONE
};
enum State current = STATE_READY;
current = STATE_DONE;
- STATE_READY
- 0
- STATE_RUNNING
- 4
- STATE_DONE
- 5
Assigning STATE_DONE does not print a label or require the program to pass through STATE_RUNNING first. A state machine needs additional transition rules. The enumeration supplies names and a type; your interface supplies the allowed states and transitions.
2. C17 constants and enum objects have distinct types
In C17, enumeration constants have type int, and their defining values must be representable as int. The enumerated type itself is compatible with an implementation-chosen char, signed integer, or unsigned integer type that can represent its members. Do not infer its byte size or signedness merely from the spelling enum.
Enumeration constants are ordinary identifiers, not C++ scoped names such as State::READY. Prefixes such as STATE_ help avoid collisions in shared scopes. Integer promotions and conversions can occur; an enum declaration is not a runtime membership test.
3. Validate external values before accepting a choice
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
enum State { STATE_READY, STATE_RUNNING, STATE_DONE };
static bool state_from_int(int input, enum State *result)
{
if (result == NULL) return false;
switch (input) {
case STATE_READY: *result = STATE_READY; return true;
case STATE_RUNNING: *result = STATE_RUNNING; return true;
case STATE_DONE: *result = STATE_DONE; return true;
default: return false;
}
}
int main(void)
{
enum State current = STATE_READY;
assert(!state_from_int(99, ¤t));
assert(current == STATE_READY);
assert(state_from_int(STATE_DONE, ¤t));
assert(current == STATE_DONE);
return 0;
}
The function accepts only the explicitly recognized choices. An invalid input leaves the caller’s object unchanged. This is a contract chosen by the program, not an effect of naming the parameter with an enum type. Checking the incoming integer also avoids treating a conversion to an enum as if it validated a network field, file value, or user input.
A default branch can handle unrecognized values in a switch. C does not generally require a switch to cover every named enumerator. Compiler warnings can help notice missing cases, but the data contract must still define what happens with unexpected input and newly added states.
4. Different names may have equal values
enum Result { RESULT_OK = 0, RESULT_SUCCESS = 0, RESULT_ERROR = 1 };
enum Result status = RESULT_SUCCESS;
Explicit enumerators can share a value. That may express a compatibility alias, but the runtime integer value cannot tell which spelling the author originally used. Two switch case labels with the same converted value are not allowed in one switch. Choose distinct values when the alternatives must be distinguishable.
5. C23 expands the enum model
A fixed underlying type helps make a deliberate interface choice. It still does not define byte order for a file format, validate an arbitrary external integer, or add C++ scoped-enum semantics. Keep representation choices, accepted values, and transition rules as separate decisions.
Primary references and further reading
- WG14 N1570 — public C11 committee draft: 6.7.2.2, 6.3.1.1, 6.8.4.2. Stable core rules used for the C17 baseline; this is not the final ISO publication.
- WG14 N3096 — public C23 working draft: 6.7.2.2p11–p15. C23 notes are separate from the C17 example checks.
Before you move on
- Enumeration constants provide meaningful names for choices.
- The language rules for values and types differ in specific C17 and C23 cases.
- External integers still need validation against the application’s accepted choices.
Keep these ideas separate
- Enum representation / Valid application state
- A representable or convertible integer does not by itself establish that the application accepts the corresponding state.
Why should a menu command read from a file be checked before treating it as a named enum choice?
Optional: say it aloud or keep a note. You can continue whenever you are ready.Check your understanding
Optional. Answers are not saved or sent, and reading stays unrestricted.
Read the explanation for every choice
- Yes, an enum ignores explicitly supplied values.
Explicit enumerator initializers are meaningful.
- Its value changes each time it is read.
An enumerator is a named integer constant, not a runtime counter.
- No. BUSY is explicitly given the value 7.
✓ Correct reasoning. Sequential defaults do not override an explicit initializer.
C17
Ready for the next idea?
Mark this chapter as read whenever you feel ready.