Sai A Sai A
Updated date Nov 07, 2023
In this blog, we will learn how to cast enums to integers in C using various methods, including type casting, implicit casting, lookup tables, and switch statements.
  • 1.1k
  • 0
  • 0

Introduction:

C is a powerful and versatile programming language, but sometimes you may encounter situations where you need to convert an enum to an integer. This conversion can be useful for various reasons, such as performing arithmetic operations or storing enum values in a data structure that only accepts integers. In this blog, we will explore different methods to cast enums to integers in C.

Method 1: Basic Type Casting

The easy way to cast an enum to an integer is by using basic type casting. You can do this by specifying the integer type you want to cast to in parentheses followed by the enum variable or value.

#include <stdio.h>

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

int main() {
    enum Weekday today = Wednesday;
    int intVal = (int)today;
    printf("Today is day %d\n", intVal);
    return 0;
}

Output:

Today is day 3

In this example, we have defined an enum named Weekday with days of the week as its values. We assigned the value Wednesday to the variable today, and then we cast it to an integer using (int)today. As a result, the output displays the integer value of Wednesday, which is 3.

Method 2: Implicit Type Casting

In C, an enum's underlying type is typically an integer, and you can implicitly cast an enum to an integer without the need for explicit casting.

#include <stdio.h>

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

int main() {
    enum Weekday today = Wednesday;
    int intVal = today; // Implicit casting
    printf("Today is day %d\n", intVal);
    return 0;
}

Output:

Today is day 3

Here, we've used the same enum and variable as in the previous example. By simply assigning today to intVal, the compiler implicitly converts the enum to an integer. The output is identical to Method 1.

Method 3: Using Enum Constants Directly

You can also directly use the enum constants in arithmetic operations without casting. C automatically treats enum values as integers in such situations.

#include <stdio.h>

enum Month {
    January,
    February,
    March,
    April,
    May
};

int main() {
    enum Month thisMonth = March;
    int nextMonth = thisMonth + 1;
    printf("This month is %d, next month is %d\n", thisMonth, nextMonth);
    return 0;
}

Output:

This month is 2, next month is 3

In this example, we have an enum representing months of the year. We directly use the enum constants in an arithmetic operation (thisMonth + 1) without any casting. The compiler treats the enum value as an integer, and we get the expected result.

Method 4: Using a Lookup Table

In some cases, you may need to map enum values to specific integers. You can use a lookup table to achieve this.

#include <stdio.h>

enum Color {
    Red,
    Green,
    Blue
};

int main() {
    enum Color myColor = Green;
    int colorMap[] = {1, 2, 3}; // Mapping enum values to integers
    int intVal = colorMap[myColor];
    printf("My favorite color is %d\n", intVal);
    return 0;
}

Output:

My favorite color is 2

In this example, we have an enum for colors, and we create a lookup table (colorMap) that maps enum values to specific integers. We access the integer value of myColor by indexing the lookup table. This method allows you to map enums to integers in a controlled way.

Method 5: Using a Switch Statement

A switch statement can be a powerful tool for casting enums to integers and performing different actions based on enum values.

#include <stdio.h>

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

int main() {
    enum Day today = Monday;
    int intVal;

    switch (today) {
        case Sunday:
            intVal = 0;
            break;
        case Monday:
            intVal = 1;
            break;
        case Tuesday:
            intVal = 2;
            break;
        case Wednesday:
            intVal = 3;
            break;
        case Thursday:
            intVal = 4;
            break;
        case Friday:
            intVal = 5;
            break;
        case Saturday:
            intVal = 6;
            break;
        default:
            intVal = -1;
            break;
    }

    printf("Today is day %d\n", intVal);
    return 0;
}

Output:

Today is day 1

In this example, we use a switch statement to map enum values to integers. We define cases for each enum value, providing the corresponding integer value. When today is Monday, the output displays the integer value 1.

Conclusion:

Casting enums to integers in C is a simple task that you may encounter in various programming scenarios. We have explored different methods, including basic type casting, and implicit casting, using enum constants directly in arithmetic operations, creating lookup tables, and utilizing switch statements. 

Comments (0)

There are no comments. Be the first to comment!!!