C Enumerations

An enumeration (abbreviated as enum) is a user-defined data type that consists of a set of named integer constants. Instead of using raw numbers like 0, 1, 2 throughout your code, enumerations let you give meaningful names to each value, making your code much easier to read and maintain.

The keyword to create an enumeration is enum.

The Problem Enumerations Solve

Imagine you write a program that tracks the days of the week using numbers 0–6. Without enumerations, this requires you to remember that 0 = Sunday, 1 = Monday, and so on. Looking at the code later, you would see numbers with no context.

// Without enum — confusing
int today = 3;   // what does 3 mean? Wednesday? Thursday?
if (today == 3) { printf("Meeting day\n"); }

// With enum — clear
enum Day { SUN, MON, TUE, WED, THU, FRI, SAT };
enum Day today = WED;   // immediately readable
if (today == WED) { printf("Meeting day\n"); }

Declaring an Enumeration

Syntax

enum enum_name { constant1, constant2, constant3, ... };

Example

enum Color { RED, GREEN, BLUE };

By default, the first name gets the value 0, the second gets 1, the third gets 2, and so on.

RED   = 0
GREEN = 1
BLUE  = 2

Declaring Enum Variables

#include <stdio.h>

enum Color { RED, GREEN, BLUE };

int main()
{
    enum Color paint = GREEN;   // paint holds the value 1

    printf("Color value: %d\n", paint);   // Output: 1

    if (paint == GREEN)
        printf("Paint is green\n");   // Output: Paint is green

    return 0;
}

Custom Values in Enumerations

You can assign specific integer values to enum constants. Subsequent constants without an assigned value continue incrementing from the last assigned value.

#include <stdio.h>

enum Priority {
    LOW    = 1,
    MEDIUM = 5,
    HIGH   = 10
};

int main()
{
    enum Priority task = HIGH;
    printf("Priority level: %d\n", task);   // Output: 10
    return 0;
}

Partial Custom Values

enum StatusCode {
    OK      = 200,
    CREATED = 201,    // 200 + 1 if not assigned, but here it's explicit
    NOT_FOUND = 404,
    ERROR   = 500
};

Auto-increment after a custom value

enum Level {
    BEGINNER = 10,
    INTERMEDIATE,   // automatically 11
    ADVANCED        // automatically 12
};

Enum in switch Statements

Enumerations work very naturally with switch statements, creating clean and readable decision logic.

#include <stdio.h>

enum Season { SPRING, SUMMER, AUTUMN, WINTER };

int main()
{
    enum Season current = SUMMER;

    switch (current)
    {
        case SPRING:
            printf("Flowers blooming\n");
            break;
        case SUMMER:
            printf("Hot and sunny\n");
            break;
        case AUTUMN:
            printf("Leaves falling\n");
            break;
        case WINTER:
            printf("Cold and snowy\n");
            break;
    }

    return 0;
}

Output:

Hot and sunny

Enum with typedef

Using typedef with an enum removes the need to write enum every time you declare a variable.

#include <stdio.h>

typedef enum {
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY
} Day;

int main()
{
    Day today = FRIDAY;   // no need to write 'enum Day'

    if (today == FRIDAY)
        printf("Weekend is near!\n");   // Output: Weekend is near!

    return 0;
}

Practical Example: Traffic Light System

#include <stdio.h>

typedef enum {
    RED,
    YELLOW,
    GREEN
} TrafficLight;

void showSignal(TrafficLight signal)
{
    switch (signal)
    {
        case RED:    printf("STOP\n");   break;
        case YELLOW: printf("READY\n");  break;
        case GREEN:  printf("GO\n");     break;
    }
}

int main()
{
    showSignal(RED);     // Output: STOP
    showSignal(GREEN);   // Output: GO
    showSignal(YELLOW);  // Output: READY
    return 0;
}

Enum Underlying Type

Enum constants are actually integers. You can use them in arithmetic, comparisons, and print them as integers.

#include <stdio.h>

enum Month { JAN=1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC };

int main()
{
    enum Month m = MAR;
    printf("Month number: %d\n", m);   // Output: 3

    // Check if the month is in first quarter
    if (m >= JAN && m <= MAR)
        printf("First quarter\n");   // Output: First quarter

    return 0;
}

Enum vs #define

Both enum and #define create named constants. Enumerations are generally preferred:

Featureenum#define
Type safetyYes — has a specific typeNo — just text replacement
Debugger supportYes — names visible in debuggerNo — only numbers shown
ScopeFollows C scoping rulesGlobal (no scope)
GroupingRelated constants grouped togetherSeparate #define for each
ReadabilityHigh — clear intentMedium

Common Mistakes with Enumerations

MistakeExampleExplanation
Duplicate valuesenum {A=1, B=1}Valid in C but can cause confusion
Out-of-range assignmentColor c = 99;Compiler accepts it but it's logically wrong
Redefining enum nameDeclaring two enums with same nameCauses a redefinition error

Summary

Enumerations in C create a set of named integer constants using the enum keyword. By default, the first constant gets value 0 and each subsequent constant increments by 1. You can assign custom integer values to any or all constants. Enum values are used as regular integers and work perfectly in switch statements and comparisons. Using typedef with enumerations makes variable declarations cleaner. Enumerations improve code readability by replacing meaningless numbers with descriptive names, and they are preferred over #define constants because they support type checking and are visible in debuggers.

Leave a Comment

Your email address will not be published. Required fields are marked *