Skip to main content
An enumeration (enum) is a user-defined data type consisting of a set of named integer constants known as enumerators. In C, individual enumerators are strictly of type int, while the enumeration type itself is represented by an implementation-defined integer type capable of holding all defined enumerator values.

Syntax

  • tag_name: An optional identifier that names the enumeration type.
  • enumerator_list: A comma-separated list of identifiers. A trailing comma is permitted in modern C (C99 onwards).
  • variable_list: An optional comma-separated list of variable instances declared immediately alongside the type definition.

Declaration Patterns

1. Tagged Declaration Defines the enumeration type using a tag. Subsequent variable declarations must include the enum keyword.
2. Anonymous Declaration Omits the tag. Variables must be declared immediately within the statement, or the declaration serves solely to inject the enumerator constants into the current scope.
3. Typedef Declaration Aliases the enumeration type to a new identifier, eliminating the need to use the enum keyword during variable instantiation.

Enumerator Value Assignment

By default, the compiler assigns the integer value 0 to the first enumerator and increments the value by 1 for each subsequent enumerator. This behavior can be overridden by assigning explicit integer constant expressions.
Note: Multiple enumerators within the same declaration are permitted to hold identical integer values.

Scope and Namespace Rules

  • Enumerator Scope: Enumerators are injected directly into the enclosing scope of the enum declaration. C does not support scoped enumerations; accessing an enumerator via its tag (e.g., State.IDLE) is a syntax error. The identifier is simply IDLE.
  • Identifier Uniqueness: Because enumerators leak into the enclosing scope, two different enumerations within the same scope cannot share an enumerator identifier.
  • Tag Namespace: The tag_name occupies the C tag namespace (shared with struct and union). It can share an identifier with a variable or function, but it cannot share an identifier with another struct, union, or enum tag in the same scope.
Tired of Poor C Skills? Fix That With Deep Grasping!Learn More