C Programs Tutorials | IT Developer
IT Developer

C Programming - C Enumeration



Share with a Friend

C Programming - C Enumeration

C Enumeration ( enum)

In C, an enumeration is a user-defined data type that consists of integral constants, making the code more readable and organized. It is declared using the enum keyword, and the values within the enumeration are automatically assigned integer values starting from 0 by default.

Syntax

C

enum enumeration_name {

    constant1,

    constant2,

    constant3,

    // ...

};

  • enumeration_name: Optional name for the enumeration.
  • constant1, constant2, ...: Named integral constants.

Key Features

  1. Improved Readability:
    • Replaces numerical values with meaningful names.
  2. Default Value Assignment:
    • Values start from 0 and increment by 1 unless explicitly assigned.
  3. Type Safety:
    • Enforces usage of predefined constants.

Example: Enumeration Basics

C

#include <stdio.h>

// Define an enumeration

enum Day {

    SUNDAY,

    MONDAY,

    TUESDAY,

    WEDNESDAY,

    THURSDAY,

    FRIDAY,

    SATURDAY

};

int main() {

    enum Day today;

    today = WEDNESDAY;

    printf("Today is day number %d\n", today);

    return 0;

}

Output:

Today is day number 3

Custom Values in Enumeration

You can explicitly assign values to some or all constants in an enumeration.

C

#include <stdio.h>

enum Status {

    SUCCESS = 1,

    FAILURE = -1,

    PENDING = 0

};

int main() {

    enum Status currentStatus;

    currentStatus = FAILURE;

    printf("Current status: %d\n", currentStatus);

    return 0;

}

Output:

Current status: -1

Enumerations with Switch Statements

Enumerations are often used in switch statements for better code clarity.

C

#include <stdio.h>

enum TrafficLight {

    RED,

    YELLOW,

    GREEN

};

int main() {

    enum TrafficLight light = GREEN;

    switch (light) {

        case RED:

            printf("Stop\n");

            break;

        case YELLOW:

            printf("Ready\n");

            break;

        case GREEN:

            printf("Go\n");

            break;

        default:

            printf("Invalid\n");

    }

    return 0;

}

Output:

Go

Advantages of enum

  1. Improved Code Clarity:
    • Descriptive names replace arbitrary numerical values.
  2. Reduced Errors:
    • Using named constants minimizes the chance of errors.
  3. Ease of Maintenance:
    • Changes to the constants’ values affect only the enum definition.

Limitations of enum

  1. Lack of Type Safety:
    • Enum values are treated as integers, which can lead to unintended usage.
  2. Limited Functionality:
    • No support for strings or non-integer types.
  3. Cannot Be Extended:
    • Once defined, an enum cannot be modified without recompiling the code.

Using Typedef with enum

You can combine typedef with enum for simpler syntax.

C

#include <stdio.h>

typedef enum {

    SPRING,

    SUMMER,

    FALL,

    WINTER

} Season;

int main() {

    Season current = FALL;

    printf("The current season is %d\n", current);

    return 0;

}

Output:

The current season is 2

Conclusion

Enumerations are a convenient way to define and manage integral constants in C programs. They enhance code readability, reduce the likelihood of errors, and are commonly used in applications requiring state management or symbolic representation of constant values.