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) in C is a user-defined data type consisting of a set of named integer constants known as enumerators. While the C compiler defaults to assigning sequential integer values starting from 0, developers can explicitly assign arbitrary constant integer expressions to any or all enumerators during declaration.

Syntax

Explicit values are assigned using the assignment operator (=) followed by a constant integer expression immediately after the enumerator identifier.
enum EnumName {
    ENUMERATOR_A = constant_expression,
    ENUMERATOR_B = constant_expression,
    ENUMERATOR_C // Implicitly assigned
};

Evaluation Rules and Mechanics

When explicitly assigning values to enumerators, the C compiler enforces the following mechanical rules:
  1. Constant Expressions: The assigned value must be a compile-time integer constant expression. It cannot be a variable or the result of a runtime function call.
  2. Sequential Incrementing: If an enumerator is declared without an explicit assignment, the compiler automatically assigns it a value equal to the preceding enumerator’s value plus one (+ 1).
  3. Negative Values: Enumerators can be explicitly assigned negative integer values. Subsequent unassigned enumerators will increment upwards toward zero.
  4. Non-Unique Values: C permits multiple enumerators within the same enumeration to hold the exact same integer value.
  5. Non-Linearity: Explicit values do not need to be declared in ascending or descending order. The compiler evaluates each assignment independently.

Code Visualization

The following block demonstrates how the compiler evaluates a mix of explicit and implicit enumerator values:
enum MixedValues {
    VAL_A = 10,       // Explicitly assigned 10
    VAL_B = 50,       // Explicitly assigned 50
    VAL_C,            // Implicitly assigned 51 (VAL_B + 1)
    VAL_D = -5,       // Explicitly assigned -5
    VAL_E = -5,       // Explicitly assigned -5 (Duplicate value allowed)
    VAL_F,            // Implicitly assigned -4 (VAL_E + 1)
    VAL_G = 0         // Explicitly assigned 0
};

Type Constraints and Representation

The C standard dictates that the expression defining the value of an enumeration constant must have a value representable as a standard int. Assigning an explicit value that falls outside the representable range of an int is a constraint violation and will result in a compilation error. Consequently, all individual enumeration constants are strictly of type int. While the enumerators themselves are always of type int, the compiler selects an implementation-defined underlying integer type for the enumeration type as a whole (such as char, signed int, or unsigned int). The compiler guarantees that this chosen underlying type is capable of representing the values of all the enumerators defined within that specific enum.
Master C with Deep Grasping Methodology!Learn More