liveBook · Manning

Enum

As explained in Modern C, Third Edition

An enumeration is a C type that defines a set of named integer values.

An enumeration defines a C type with named integer values. Constants may receive positional values starting at zero or explicit values. section 5.6.2

Name values and index an array

enum corvid { magpie, raven, jay, chough, corvid_num }; char const*const bird[corvid_num] = { [chough] = "chough", [raven] = "raven", [magpie] = "magpie", [jay] = "jay", }; The enumeration fixes the indices, so array entries can be written in a different order and a new corvid can be inserted before corvid_num. section 5.6.2

Keep these rules in mind:

  • Treat enumeration constants as the main practical benefit for small values; enumeration objects convert to signed or unsigned types during indexing and arithmetic. section 5.6.2
  • Use integer constant expressions for enumeration values; ordinary objects such as const-qualified variables cannot participate. section 5.6.2

How Modern C, Third Edition covers it

How deep it goes
Explains positional and explicit values, conversions, and integer constant expressions, but does not develop enumeration storage mechanics.
Examples
Uses corvid names, a designated string array, sequential constants, and valid or invalid constant-expression definitions.
What you can do after
Apply it via named constants and designated array indices while respecting compile-time value constraints.
How it connects
Connects enumerations to arrays, designated initializers, arithmetic conversions, and integer constant expressions.

Also explained in

Compare how 23 books explain enum →

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.