Skip to content
  • Enumerations · C

Enumerations

LearnViaNotes @learnvianotes· 2 min read

An enum defines a set of named integer constants — readable names for states, options and error codes.

C
#include <stdio.h>

enum Weekday { MON, TUE, WED, THU, FRI, SAT, SUN };        /* 0, 1, 2, ... */
enum Http { OK = 200, CREATED, NOT_FOUND = 404, TEAPOT = 418 };  /* CREATED = 201 */

static const char *names[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};

int main(void) {
    enum Weekday d = FRI;
    printf("%d %s\n", d, names[d]);
    printf("%d %d %d\n", OK, CREATED, NOT_FOUND);
    for (enum Weekday w = SAT; w <= SUN; w++) printf("%s ", names[w]);
    printf("\nsizeof(enum Weekday) = %zu\n", sizeof(enum Weekday));
    return 0;
}
Output
4 Fri
200 201 404
Sat Sun
sizeof(enum Weekday) = 4

Idioms

C
#include <stdio.h>

/* a trailing COUNT enumerator gives the number of values for free */
typedef enum { RED, GREEN, BLUE, COLOR_COUNT } Color;

/* X-macro: one list generates both the enum and its names */
#define STATES(X) X(IDLE) X(RUNNING) X(DONE)
#define AS_ENUM(n) n,
#define AS_NAME(n) #n,
typedef enum { STATES(AS_ENUM) STATE_COUNT } State;
static const char *state_names[] = { STATES(AS_NAME) };

int main(void) {
    int hits[COLOR_COUNT] = {0};
    hits[GREEN]++;
    printf("colors: %d, green hits: %d\n", COLOR_COUNT, hits[GREEN]);
    for (int s = 0; s < STATE_COUNT; s++) printf("%d=%s ", s, state_names[s]);
    printf("\n");
    return 0;
}
Output
colors: 3, green hits: 1
0=IDLE 1=RUNNING 2=DONE
enum#define
Scoped like variablesGlobal text substitution
Visible in the debugger by nameGone after preprocessing
Auto-numberingEvery value by hand
switch can warn about missing casesNo checking
Only int values (C23 allows a fixed underlying type)Any value/type (strings, doubles)

What is the default value of the first enumerator, and of the ones after it?

What is the size of an enum?

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report