Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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

enum [tag_name] {
    enumerator_1 [= constant_expression],
    enumerator_2 [= constant_expression],
    /* ... */
} [variable_list];
  • 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.
enum State {
    IDLE,
    RUNNING,
    HALTED
};

enum State current_state;
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.
enum {
    READ_MODE,
    WRITE_MODE
} file_mode;
3. Typedef Declaration Aliases the enumeration type to a new identifier, eliminating the need to use the enum keyword during variable instantiation.
typedef enum {
    TCP,
    UDP
} Protocol;

Protocol network_proto;

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.
enum Configuration {
    MAX_CONNECTIONS = 100,  // Explicitly 100
    TIMEOUT = 50,           // Explicitly 50
    RETRY_LIMIT,            // Implicitly 51 (TIMEOUT + 1)
    DEFAULT_PORT = 8080,    // Explicitly 8080
    ALT_PORT                // Implicitly 8081 (DEFAULT_PORT + 1)
};
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.
Master C with Deep Grasping Methodology!Learn More