LearnContact
Lesson 257 min read

Enumerations

Learn how Enumerations allow you to assign meaningful names to fixed sets of integer values instead of using confusing numbers.

Introduction

In many programs, a variable can only have one value from a fixed set of predefined options, such as Days of the Week, Traffic Signal Colors, or Order Status. One way to represent these is by using integers, such as 0 for Sunday and 1 for Monday. Although this works, remembering what each number represents can become difficult. To make programs easier to read, C provides Enumerations (enum).

What is an Enumeration?

An Enumeration (enum) is a user-defined data type that consists of a set of named integer constants. Each named constant is called an Enumerator. Instead of using numbers directly, programmers use descriptive names, making the code more readable.

Why Do We Need Enumerations?

Without Enumerations

With Enumerations

Real-World Analogy

Imagine a traffic signal. Instead of saying "0, 1, 2", we naturally say "Red, Yellow, Green". The names are much easier to understand than numbers. Similarly, enumerations replace numbers with meaningful names.

Traffic Signal
0 → Red    (Stop)
1 → Yellow (Wait)
2 → Green  (Go)

// With Enum:
RED
YELLOW
GREEN

Characteristics of Enumerations

Key Characteristics
  • User-defined data type
  • Store named integer constants
  • Improve readability
  • Represent fixed values
  • Underlying values are always integers

Enumerator Values

By default, enumerators receive integer values starting from 0. The compiler automatically assigns these values.

Default Assignment
Sunday     → 0
Monday     → 1
Tuesday    → 2
Wednesday  → 3
...

Programmers can also assign custom integer values. For example: Pending = 100 and Approved = 200.

Declaring and Creating Enumerations

An enumeration is declared using the enum keyword. This creates a new user-defined data type. After defining it, variables can be created that store the values defined in that enumeration.

Syntax
enum EnumName
{
    Enumerator1,
    Enumerator2
};

// Creating variable:
enum EnumName var1;

Example 1: Creating an Enumeration

c
#include <stdio.h>

enum Day
{
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday
};

int main()
{
    enum Day today;

    today = Wednesday;

    printf("%d", today);

    return 0;
}

Line-by-Line Explanation

Lines 3-11: enum Day { ... }

Defines a new enumeration named Day. The compiler automatically assigns Sunday = 0, Monday = 1, and so on. Wednesday receives the value 3.

Line 15: enum Day today;

Creates an enumeration variable named today.

Line 17: today = Wednesday;

Assigns the enumerator Wednesday to the variable. Internally, this represents the integer value 3.

Line 19: printf("%d", today);

Displays the integer value associated with Wednesday. The output is 3.

Memory Representation

Variable: today
+-----------+
|     3     |
+-----------+

Meaning: Wednesday

// Although the program uses the name "Wednesday",
// the value stored in memory is the integer 3.

Program Execution Flow

Program Starts
Define Enumeration
Create Variable
Assign Wednesday (3)
Display Value → Program Ends

Example 2: Enumeration with Custom Values

You can explicitly assign integer values to enumerators.

c
#include <stdio.h>

enum Status
{
    Pending = 100,
    Approved = 200,
    Rejected = 300
};

int main()
{
    enum Status orderStatus;
    orderStatus = Approved;

    printf("%d", orderStatus); // Output: 200

    return 0;
}

Advantages & Limitations

Advantages

Limitations

Real-World Applications

Traffic Systems

Red, Yellow, Green.

Days/Months

Sunday, Monday, January, February, and other predefined calendar values.

User Roles

Admin, Manager, Employee, Guest.

Order Status

Pending, Approved, Rejected.

Game Difficulty

Easy, Medium, Hard.

Enumeration vs Constants

FeatureEnumerationConstants (#define)
GroupingGroups related named valuesRepresents individual fixed values
ReadabilityImproves readability for setsGood for single values such as PI and MAX
UsageUsed for predefined optionsUsed for fixed configuration values

Common Beginner Mistakes

Assuming Enumerations Store Strings

Enumerations store integer values, not text. Wednesday is simply a meaningful name associated with the integer value 3.

Forgetting Default Values Start from Zero

If no value is assigned, the first enumerator starts at 0, not 1.

Using Magic Numbers Instead of Enumerators

Prefer today = Wednesday; instead of today = 3;. This makes the code easier to understand.

Assigning Unrelated Values

Enumeration variables should represent the predefined enumerator values defined in the enum.

Best Practices

  • Use meaningful enumeration names, such as enum Day and enum Status.
  • Group related constants together.
  • Avoid unnecessary custom values unless required.
  • Prefer enumerators over hardcoded numbers.
  • Keep enumeration names descriptive and consistent.

Frequently Asked Questions

What is an enumeration?

An enumeration is a user-defined data type that represents a set of named integer constants.

What is an enumerator?

An enumerator is a named constant defined inside an enumeration, such as Sunday or Monday.

What is the default value of the first enumerator?

The first enumerator has a default value of 0, unless another value is explicitly assigned.

Can enumerators have custom values?

Yes. You can explicitly assign integer values to enumerators, such as Pending = 100.

Why should I use enumerations?

Enumerations make programs more readable, easier to maintain, and eliminate the need for hardcoded numeric values.

Key Takeaways

  • An enumeration is a user-defined data type.
  • Enumerations contain named integer constants called enumerators.
  • By default, enumerator values begin at 0.
  • Custom integer values can also be assigned.
  • Enumerations improve readability and reduce the use of magic numbers.
  • They are widely used to represent predefined sets of related values.

Summary

Enumerations (enum) provide a simple and effective way to represent fixed sets of related values using meaningful names instead of hardcoded numbers. This improves code readability, maintainability, and reduces programming errors. Whether representing days of the week, traffic signals, user roles, or application states, enumerations help make programs easier to understand and maintain.

Next Lesson →

Storage Classes