Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

enum — enumerated type

enum defines an enumerated type and named integer constants. C23 also allows an enumeration's underlying type to be specified explicitly.

Version: C23; repository compiler flag, production runtime not attested

Syntax

enum Tag { NAME, OTHER = constant_expression };
enum Tag variable;
/* C23: fixed underlying type */
enum Tag2 : unsigned int { VALUE = 1 };

enum defines an enumerated type with named constants. In enum State, State is a type tag; without a separate typedef, State alone does not replace enum State.

If the first enumerator in the list has no initializer, its value is 0. Any later enumerator without an initializer has the value of the immediately preceding enumerator plus one. = sets a value using an integer constant expression. Different constants may share a value. Their names belong to the ordinary identifier namespace, not a separate enumeration scope.

C23 provides a fixed underlying type: enum State : unsigned int { ... };. Values must fit that type, and the constants have the enumerated type. Without an explicit underlying type, do not assume every enumeration has the size or representation of int; in C23, constants are not always of type int either.

The example uses C23 syntax. A repository compiler flag alone does not attest support for this feature or operation in a production environment.

Examples

#include <stdio.h>

enum State : unsigned int {
    STATE_IDLE,
    STATE_RUNNING = 4,
    STATE_DONE
};

int main(void)
{
    enum State state = STATE_DONE;
    printf("%u %u %u\n", (unsigned int)STATE_IDLE,
           (unsigned int)STATE_RUNNING, (unsigned int)state);
    return 0;
}

Result

0 4 5

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources