Enum in C

Enum in C is a user-defined type used to represent a fixed set of named integer constants. It is one of the cleanest ways to make code more readable when a variable is meant to hold one value out of a limited known set, such as days of the week, states of a machine, menu choices, colors, or error codes.

Many beginners first see enums as just a shorter way to write numbers, but that misses the real benefit. The main value of enum in C is that it replaces unclear magic numbers with meaningful names. Instead of remembering that 2 means BLUE or MONDAY, you can use readable enumerator names directly. In this article, we will understand what enum in C is, how it works, default and custom enumerator values, enum variables, enum with switch, common use cases, and the mistakes beginners should avoid.

What is Enum in C?

An enum in C is a user-defined type that contains a set of named integer constants. The keyword used to declare it is enum.

For example:

enum Color {
    RED,
    GREEN,
    BLUE
};

Here, RED, GREEN, and BLUE are named constants called enumerators. By default, C assigns them integer values starting from zero.

  • RED = 0
  • GREEN = 1
  • BLUE = 2

An enum gives meaningful names to related integer values.

Why Enum is Important in C

  • It makes code more readable.
  • It replaces unexplained numeric values with names.
  • It helps represent fixed states clearly.
  • It improves maintenance and reduces confusion.
  • It works well with control logic such as switch statements.

If you see status = ERROR, the meaning is immediately clearer than status = 3. That is the practical value of enums.

Syntax of Enum in C

The general syntax of enum in C is:

enum enum_name {
    enumerator1,
    enumerator2,
    enumerator3
};

After declaring the enum, you can create variables of that enum type.

enum Color favorite;
favorite = GREEN;

This means favorite is intended to hold one of the values defined in the Color enum.

Default Values in Enum in C

By default, the first enumerator gets the value 0, and each next one increases by 1.

EnumeratorDefault Value
RED0
GREEN1
BLUE2

For example:

enum Day {
    SUNDAY,
    MONDAY,
    TUESDAY
};

Here, SUNDAY becomes 0, MONDAY becomes 1, and TUESDAY becomes 2.

Custom Values in Enum in C

You are not forced to use only the default sequence. You can assign values explicitly to enumerators.

enum Status {
    OK = 1,
    WARNING = 5,
    ERROR = 10
};

You can also mix explicit and automatic assignment.

enum Number {
    ONE = 1,
    TWO,
    THREE,
    TEN = 10,
    ELEVEN
};
EnumeratorValue
ONE1
TWO2
THREE3
TEN10
ELEVEN11

Once a value is explicitly assigned, the next enumerator continues from there.

Declaring Enum Variables in C

After defining an enum, you can declare variables of that enum type.

enum Day today;
today = MONDAY;

You can also combine declaration and variable creation in one statement.

enum Day {
    SUNDAY,
    MONDAY,
    TUESDAY
} today;

Although enum variables are conceptually special, they are still based on integer values internally.

How Enum is Stored in C

An enum in C is represented internally using integer values. That means enumerators are named integer constants, and enum variables usually behave closely to integer-based values in expressions.

For example:

#include <stdio.h>

enum Level {
    LOW,
    MEDIUM,
    HIGH
};

int main(void)
{
    enum Level current = HIGH;
    printf("%d
", current);
    return 0;
}

This prints the integer value of HIGH, which is 2 in this case.

So enums improve readability, but they do not magically become text labels during output. If you want readable text such as HIGH on the screen, you must map the enum value manually.

Enum with Switch Statement in C

Enums work very naturally with switch statements because both are based on discrete values.

#include <stdio.h>

enum Day {
    SUNDAY,
    MONDAY,
    TUESDAY
};

int main(void)
{
    enum Day today = MONDAY;

    switch (today)
    {
        case SUNDAY:
            printf("Sunday
");
            break;
        case MONDAY:
            printf("Monday
");
            break;
        case TUESDAY:
            printf("Tuesday
");
            break;
    }

    return 0;
}

This style is cleaner than using raw numbers because the meaning of each case is obvious.

Common Uses of Enum in C

  • Days of the week
  • Months of the year
  • Machine states such as START, STOP, PAUSE
  • Error codes and status codes
  • Menu choices
  • Colors, modes, and categories

Enums are especially useful when the program works with a limited, predefined set of options.

Enum vs #define in C

Beginners often wonder whether enum and #define are the same. They are not. Both can represent named numeric values, but they work differently.

FeatureEnum#define
PurposeNamed related integer constantsText replacement by preprocessor
Scope styleLanguage-level declarationPreprocessor substitution
Readability for grouped valuesBetterCan become messy
Best useFixed sets such as states or categoriesGeneral macros or symbolic replacements

If you are representing a related set of named integer choices, enum is usually the cleaner design.

Common Mistakes with Enum in C

  • Thinking enum values are strings
  • Forgetting that default values start from 0
  • Assuming enum automatically prints its name instead of its integer value
  • Using enum where the values are not really from a fixed known set
  • Mixing too many unrelated concepts into one enum
MistakeWhy it is wrongCorrect understanding
printf("%s", color);Enum is not a stringEnum values are integer-based
Assuming first value is 1Default start is 0 unless assignedCheck explicit and default numbering carefully
Using one enum for unrelated valuesReduces clarityKeep each enum focused on one logical set

The biggest beginner improvement is to stop treating enum names as text labels and instead remember that they are named integer constants.

Best Practices for Enum in C

  • Use enum when a variable should hold one value from a fixed known set.
  • Choose clear enumerator names that explain meaning.
  • Assign explicit values when the numbering matters.
  • Use switch with enum values for readable control logic.
  • Keep each enum focused on one specific category.

Good use of enums makes code easier to read, easier to maintain, and less dependent on unexplained integers.

FAQs

What is enum in C?

Enum in C is a user-defined type that contains a set of named integer constants.

What are the default values of enum in C?

By default, the first enumerator gets value 0 and the following ones increase by 1.

Can we assign custom values in enum in C?

Yes. You can assign explicit values to enumerators, and the next automatic values continue from there.

Is enum in C stored as an integer?

Enum values are represented internally using integer values.

What is the difference between enum and #define in C?

Enum is a language feature for named related integer constants, while #define is a preprocessor text replacement mechanism.