Convert Enum Type Variable to String in C++



Enum is a user-defined datatype in C++ that is used to assign names to integer values. For example, you can use an enum to represent the days of the week from 0 to 6, where 0 represents Sunday, 1 represents Monday, and so on. When you try to access a value of an enum, it will only return an integer value.

In this article, we will learn how to convert an enum type variable to a string type variable in C++, so that we can easily display the enum value as a string.

Example of Enum:

enum color {
    RED,  // value 0
    GREEN,  // value 1
    BLUE  // value 2
};

In the above example, we have defined an enum named color with three values: RED, GREEN, and BLUE. The values are automatically assigned starting from 0.

Convert Enum to String in C++

Here are all the approaches to convert an enum to a string in C++ which we will be discussing along with examples:

Convert Enum to String Using Switch Case

The switch statement is a control statement that is used to check if a variable is equal to any value in a list of values. We can use this switch statement to convert an enum type variable to a string type variable by checking each case and returning the corresponding string value.

Example

In this example, we will use a switch case to convert an enum type variable to a string type variable.

#include <iostream>
#include <string>

enum color {
    RED,  // value 0
    GREEN,  // value 1
    BLUE  // value 2
};

std::string colorToString(color c) {
    switch (c) {
        case RED: return "RED";
        case GREEN: return "GREEN";
        case BLUE: return "BLUE";
        default: return "UNKNOWN";
    }
}

int main() {
    color c = GREEN;
    std::cout << "The color " << c << " is: " << colorToString(c) << std::endl;
    return 0;
}

The output of the above code will be:

The color 1 is: GREEN

Convert Enum to String Using Map

A map is a data structure that stores key-value pairs. We can use a map to store the enum values as keys and their corresponding string representations as values. Let's see an example to understand how to do this.

Example

In this example, we will use a map to convert an enum type variable to a string type variable.

#include <iostream>
#include <string>
#include <map>

enum color {
    RED,  // value 0
    GREEN,  // value 1
    BLUE  // value 2
};

std::string colorToString(color c) {
    static std::map<color, std::string> colorMap = {
        {RED, "RED"},
        {GREEN, "GREEN"},
        {BLUE, "BLUE"}
    };
    return colorMap[c];
}

int main() {
    color c = BLUE;
    std::cout << "The color " << c << " is: " << colorToString(c) << std::endl;
    return 0;
}

The output of the above code will be:

The color 2 is: BLUE

Convert Enum to String Using Magic Enum

The magic_enum is header only library that provides a way to convert enum values to strings and vice versa. This can only be used in C++17 and later versions. Let's see an example to understand how to use it.

Example

In this example, we will use the magic_enum library to convert an enum type variable to a string type variable.

#include <iostream>
#include <magic_enum.hpp>
#include <string>

enum class Color {
    RED,  // value 0
    GREEN,  // value 1
    BLUE  // value 2
};

std::string colorToString(Color c) {
    return magic_enum::enum_name(c);
}

int main() {
    Color c = Color::GREEN;
    std::cout << "The color " << c << " is: " << colorToString(c) << std::endl;
    return 0;
}

The output of the above code will be:

The color 1 is: GREEN
Note: This code will run only on C++17 and later versions and require to install the magic_enum library from github.
Farhan Muhamed
Farhan Muhamed

No Code Developer

Updated on: 2025-06-06T18:57:32+05:30

502 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements