Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. C Language Dictionary
  3. enum

enum

A data type that groups related constants under meaningful names. Instead of using plain integers for things like days of the week, directions, or status codes, an enum lets you represent a limited set of values with descriptive names, improving code readability.

Syntax

// Defines an enumeration (values are assigned starting from 0, 1, 2 ...).
enum tag_name {
    CONSTANT_1,
    CONSTANT_2,
    CONSTANT_3
};

// You can explicitly assign values.
enum tag_name {
    CONSTANT_1 = value1,
    CONSTANT_2 = value2,
    CONSTANT_3      // Assigned the previous value + 1.
};

// Declares an enum variable.
enum tag_name variable_name;
enum tag_name variable_name = CONSTANT;

// Combined with typedef, you can omit the enum keyword.
typedef enum { CONSTANT_1, CONSTANT_2 } TypeName;

Characteristics of Enumerations

FeatureDescription
Default valuesThe first constant starts at 0 and increments by 1. If you assign a value at any position, the sequence continues from there.
Internal representationEnum constants are treated as integers (int) internally. Conversion between enums and integers is implicit.
ScopeIn C, enum constants are placed in the global scope. It is common practice to use a prefix to avoid name collisions.
Use with switchBecause enum values are integers, combining them with a switch statement to branch logic for each state is a common pattern.

Sample Code

#include <stdio.h>

// An enumeration representing days of the week (MON = 0, TUE = 1, ...).
typedef enum {
    MON, TUE, WED, THU, FRI, SAT, SUN
} Weekday;

// An enumeration representing directions (can be used as bit flags).
typedef enum {
    DIR_NORTH = 0,
    DIR_EAST  = 1,
    DIR_SOUTH = 2,
    DIR_WEST  = 3
} Direction;

// An enumeration representing error codes.
typedef enum {
    ERR_OK      =  0,
    ERR_NOTFOUND = -1,
    ERR_DENIED   = -2,
    ERR_TIMEOUT  = -3
} ErrorCode;

void print_weekday(Weekday day) {
    const char *names[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
    printf("Day: %s\n", names[day]);
}

const char *direction_name(Direction d) {
    switch (d) {
        case DIR_NORTH: return "North";
        case DIR_EAST:  return "East";
        case DIR_SOUTH: return "South";
        case DIR_WEST:  return "West";
        default:        return "Unknown";
    }
}

int main(void) {
    Weekday today = WED;
    print_weekday(today); // Prints "Day: Wed".

    // Enum values can be used as integers.
    printf("Value of WED: %d\n", WED); // Prints "2".

    Direction dir = DIR_NORTH;
    printf("Direction: %s\n", direction_name(dir)); // Prints "Direction: North".

    // Using an enum with a switch statement.
    ErrorCode err = ERR_NOTFOUND;
    switch (err) {
        case ERR_OK:       printf("Success\n"); break;
        case ERR_NOTFOUND: printf("Not found.\n"); break; // This branch executes.
        case ERR_DENIED:   printf("Access denied.\n"); break;
        case ERR_TIMEOUT:  printf("Timed out.\n"); break;
    }

    // Size of the enum type (typically the same as int, but implementation-defined).
    printf("Size of Weekday: %zu bytes\n", sizeof(Weekday));

    return 0;
}

Notes

Enumerations improve readability, but they are integers under the hood. C's type checking for enums is loose — a compiler may not warn you if you accidentally assign a value from a different enum type. Because enum constant names live in the global scope, using the same name in two different enums causes a compile error. Use prefixes (e.g., "DIR_", "ERR_") to avoid collisions.

When combining enums with switch, some compilers can be configured to warn about unhandled enum constants. Enabling this warning helps you catch missing cases when you add new constants.

Use enumerations for groups of related constants. To group data of different types together, use struct. For type aliases, see typedef.

If you find any errors or copyright issues, please .