liveBook · Manning

Enum constant

As explained in Modern C, Third Edition

An enumeration constant is a named integer constant declared as a member of a C enumeration.

An enum constant is a named integer constant declared as a member of a C enumeration. Naming values prevents unrelated uses of the same number from becoming indistinguishable and lets code refer to meaning rather than position. section 5.6 section 5.6.2

Listing 5.1 An enumeration type and related array of strings
enum corvid { magpie, raven, jay, chough, corvid_num, };
char const*const bird[corvid_num] = {
  [chough] = "chough",
  [raven]  = "raven",
  [magpie] = "magpie",
  [jay]    = "jay",
};

This listing pairs an enumeration with a designated-initializer array, so the enum names—not manually tracked positions—determine the indexing relationship. section 5.6.2

The essential rules are:

  • An enumerator receives either an explicit value or a positional value; omitted values begin at 0 and increase by one. section 5.6.2
  • Adding an item before a sentinel such as corvid_num updates the resulting count without manually revising array order. section 5.6.2
  • Enumeration constants are not necessarily of the enumeration type; when every constant in a simple enumeration fits signed int, the constants have signed int type. section 5.6.2
  • Enumerator values use integer constant expressions, which cannot evaluate ordinary objects; literals, other enum constants, and constexpr names can participate. section 5.6.2

How Modern C, Third Edition covers it

How deep it goes
Explains declaration, positional and explicit values, array ordering, constant-expression constraints, and enumerator type behavior.
Examples
Uses corvid names, a sentinel count, a designated-initializer string array, and arithmetic enumerators.
What you can do after
Apply enum constants to replace repeated numeric values and keep related array indexing aligned with named cases.
How it connects
Connects enumeration constants with enumerations, designated initializers, integer constant expressions, macros, and constexpr objects.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.