Lesson 50 of 60 – Enumerations in C
83%

Enumerations in C

An enumeration, commonly called an enum, is a user-defined data type in C that allows us to create a set of named integer constants. Enums make programs easier to read because meaningful names can be used instead of numeric values.

Note: By default, the first enum member has the value 0, and each following member normally increases by 1.

1. What is an Enumeration?

An enumeration is a user-defined type containing a collection of named integer constants.

enum Day
{
    MONDAY,
    TUESDAY,
    WEDNESDAY
};

Here, MONDAY, TUESDAY, and WEDNESDAY are enumeration constants.

2. Why Use enum?

Enums make code more readable and meaningful.

For example, instead of writing:

int day = 1;

we can write:

enum Day day = TUESDAY;

The second version clearly communicates what the value represents.

3. enum Syntax

The basic syntax for defining an enumeration is:

enum EnumName
{
    constant1,
    constant2,
    constant3
};

The enum definition ends with a semicolon.

4. Defining an enum

enum Color
{
    RED,
    GREEN,
    BLUE
};

This creates an enumeration type named enum Color.

5. Declaring an enum Variable

After defining an enum, we can declare a variable of that enum type.

enum Color color;

Now the variable color can hold an enumeration value such as RED, GREEN, or BLUE.

6. Assigning an enum Value

enum Color
{
    RED,
    GREEN,
    BLUE
};

enum Color color;

color = GREEN;

The variable color now represents the named constant GREEN.

7. Default enum Values

If no values are explicitly assigned, enumeration constants normally start from 0 and increase by 1.

enum Number
{
    ZERO,
    ONE,
    TWO,
    THREE
};

The values are normally:

  • ZERO = 0
  • ONE = 1
  • TWO = 2
  • THREE = 3

8. Explicit enum Values

We can assign specific integer values to enumeration constants.

enum Status
{
    SUCCESS = 1,
    ERROR = 2,
    PENDING = 3
};

This can make the meaning of numeric status values clearer.

9. Mixed enum Values

Some enum values can be assigned explicitly while others continue from the previous value.

enum Numbers
{
    A = 10,
    B,
    C,
    D = 20,
    E
};

The values are:

  • A = 10
  • B = 11
  • C = 12
  • D = 20
  • E = 21

10. enum with switch

Enums work well with switch statements.

enum Day
{
    MONDAY,
    TUESDAY,
    WEDNESDAY
};

enum Day day = TUESDAY;

switch(day)
{
    case MONDAY:
        printf("Monday");
        break;

    case TUESDAY:
        printf("Tuesday");
        break;

    case WEDNESDAY:
        printf("Wednesday");
        break;
}

11. enum and if Statement

An enum value can also be compared using an if statement.

enum Status
{
    ACTIVE,
    INACTIVE
};

enum Status status = ACTIVE;

if(status == ACTIVE)
{
    printf("Account is active");
}

12. enum Constants are Named Constants

The names declared inside an enum represent integer constants.

enum Color
{
    RED,
    GREEN,
    BLUE
};

Here, RED, GREEN, and BLUE are enumeration constants.

13. Printing enum Values

Enum values can be printed as integers.

#include <stdio.h>

enum Color
{
    RED,
    GREEN,
    BLUE
};

int main()
{
    enum Color color = GREEN;

    printf("%d", color);

    return 0;
}

The output is normally 1 because GREEN is the second constant.

14. enum with User Input

A common approach is to read an integer and then map it to an enum value.

enum Choice
{
    ADD = 1,
    SUBTRACT = 2,
    EXIT = 3
};

int choice;

printf("Enter choice: ");
scanf("%d", &choice);

if(choice == ADD)
{
    printf("Addition selected");
}

15. enum with typedef

The typedef keyword can give a shorter name to an enum type.

typedef enum
{
    LOW,
    MEDIUM,
    HIGH
} Level;

Now we can declare a variable using:

Level level = HIGH;

16. typedef enum Example

#include <stdio.h>

typedef enum
{
    OFF,
    ON
} Power;

int main()
{
    Power status = ON;

    if(status == ON)
    {
        printf("Power is ON");
    }

    return 0;
}

17. enum for Days

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

Enums are useful when a variable can have one value from a fixed set of named choices.

18. enum for Months

enum Month
{
    JANUARY = 1,
    FEBRUARY,
    MARCH,
    APRIL,
    MAY,
    JUNE
};

Starting January at 1 can make the values convenient for applications that use month numbers.

19. enum in a Structure

An enum can be used as a member of a structure.

enum Gender
{
    MALE,
    FEMALE,
    OTHER
};

struct Student
{
    int id;
    enum Gender gender;
};

This allows the structure to store a named category.

20. enum with Functions

An enum can be passed to a function like other C values.

enum Status
{
    ACTIVE,
    INACTIVE
};

void displayStatus(enum Status status)
{
    if(status == ACTIVE)
    {
        printf("Active");
    }
    else
    {
        printf("Inactive");
    }
}

21. Pointer to enum

A pointer can store the address of an enum variable.

enum Color
{
    RED,
    GREEN,
    BLUE
};

enum Color color = BLUE;

enum Color *ptr = &color;

printf("%d", *ptr);

The dereference operator * accesses the value stored at the address.

22. enum Array

An array can contain enum values.

enum Color
{
    RED,
    GREEN,
    BLUE
};

enum Color colors[3] =
{
    RED,
    GREEN,
    BLUE
};

Each array element stores an enum value.

23. enum for Menu Options

enum Menu
{
    ADD = 1,
    VIEW,
    UPDATE,
    DELETE,
    EXIT
};

This is useful for programs that contain menus with fixed choices.

enum Menu choice = VIEW;

24. enum for Traffic Lights

enum TrafficLight
{
    RED,
    YELLOW,
    GREEN
};

enum TrafficLight signal = GREEN;

if(signal == GREEN)
{
    printf("Go");
}

Enums can represent states with a small, fixed set of meaningful names.

25. Common Mistake: Duplicate Enumerator Names

Enumerator names share the same identifier namespace as other ordinary identifiers at the same scope. Therefore, the same enumerator name should not be declared again in a conflicting scope.

enum First
{
    VALUE
};

/* Avoid declaring another VALUE
   in the same scope. */

26. Common Mistake: Confusing enum Names and Values

The enum type name and the enumerator names are different things.

enum Color
{
    RED,
    GREEN,
    BLUE
};

Here:

  • Color is the enum type name.
  • RED is an enumerator.
  • GREEN is an enumerator.
  • BLUE is an enumerator.

27. Complete enum Program

#include <stdio.h>

enum Operation
{
    ADD = 1,
    SUBTRACT,
    MULTIPLY,
    DIVIDE
};

int main()
{
    enum Operation operation = MULTIPLY;

    switch(operation)
    {
        case ADD:
            printf("Addition");
            break;

        case SUBTRACT:
            printf("Subtraction");
            break;

        case MULTIPLY:
            printf("Multiplication");
            break;

        case DIVIDE:
            printf("Division");
            break;
    }

    return 0;
}

28. enum and Readability

Enums improve readability by replacing unexplained numbers with meaningful names.

Without enum:

int status = 2;

With enum:

enum Status
{
    PENDING = 1,
    APPROVED = 2,
    REJECTED = 3
};

enum Status status = APPROVED;

The second version makes the meaning of the value easier to understand.

29. Practical Uses of enum

Enums are commonly useful for representing:

  • Days of the week
  • Months
  • Menu choices
  • User roles
  • Account status
  • Traffic light states
  • Game states
  • Program modes
  • Error or operation types
  • Fixed categories

30. Practice Program

Create a C program that:

  1. Defines an enum named Course.
  2. Adds C, CPP, JAVA, and PYTHON.
  3. Creates an enum variable.
  4. Assigns one course to the variable.
  5. Uses a switch statement to display the selected course.
  6. Prints the numeric value of the selected enum constant.
  7. Creates another enum for student status.
  8. Uses the status enum in an if statement.

This practice will help you understand enum definitions, variables, comparisons, and switch statements.

📌 Key Points

  • An enum is a user-defined data type in C.
  • It contains named integer constants called enumerators.
  • By default, enumeration values normally start from 0.
  • Enumeration values can be assigned explicitly.
  • Enums improve code readability.
  • Enums work well with if and switch statements.
  • An enum variable can be passed to functions.
  • An enum can be used inside a structure.
  • An array can contain enum values.
  • typedef can provide a shorter name for an enum type.
  • Enums are useful when a variable has a fixed set of meaningful choices.

🧠 Quick Quiz

Question: What is the default value of the first enumerator when no explicit value is assigned?