YOUR C LEARNING COMPANION

Find your next idea.

Learning path
Designing dataChapter 32 of 73

Enumerations: named choices

Name states clearly while separating enum types, numeric representations, accepted inputs, and C23 extensions.

5 min readCore conceptsC17 baseline
Offline reading
In this chapter8 sections
0% through this chapter

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.

C17 · illustrative fragment
enum State {
    STATE_READY,
    STATE_RUNNING = 4,
    STATE_DONE
};
enum State current = STATE_READY;
current = STATE_DONE;
enum State
STATE_READY
0
STATE_RUNNING
4
STATE_DONE
5
These are named integer choices, not strings or automatic transition rules.

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

Complete C17 program
#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, &current));
    assert(current == STATE_READY);
    assert(state_from_int(STATE_DONE, &current));
    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

C17 · illustrative fragment
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

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.
Explain it in your own words

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.

With enum State { IDLE = 2, BUSY = 7 };, must BUSY have value 3?
Read the explanation for every choice
  1. Yes, an enum ignores explicitly supplied values.

    Explicit enumerator initializers are meaningful.

  2. Its value changes each time it is read.

    An enumerator is a named integer constant, not a runtime counter.

  3. 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.

CHAPTER 32 / 73

In this chapter

Enumerations: named choices

Search all 73 chapters in both languages.EN + HG

Try “pointer”, “why int”, or “undefined behavior”.

Concepts connect. Follow your curiosity.↑ ↓ to explore · Enter to open
MAKE YOURSELF COMFORTABLE

Your reading space

Appearance

A clear canvas with balanced contrast.

Text size

100%
AA

Reading tools

Your data stays in this browser

Export a backup before changing browsers or replacing an older version of the reader.

YOUR PERSONAL MARGINS

My notebook

Saved on this device
A LITTLE CLARITY

The glossary

A starting meaning, then a chapter to go deeper.

A QUICK DEFINITION

A little clarity

Read the full explanation →

A thought worth keeping