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
