In C++, an enum (short for enumeration) is a user-defined data type that allows you to assign names to integral constants. It helps make your code more readable and organized, especially when dealing with a fixed set of related values like days of the week, directions, or months.
Enums are a great way to represent named constants instead of using plain numbers in your code.
What Is an Enum in C++?
An enum is a collection of symbolic names (called enumerators) that represent integer values.
It provides a way to define a set of constants under one type name.
Example:
enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
Here:
-
Day is the name of the enumeration.
-
Sunday, Monday, etc. are the enumerator constants.
-
By default, the first value (Sunday) is assigned 0, and each following one increments by 1.
So internally:
Sunday = 0
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
Saturday = 6
Declaring and Using Enums in C++
Enums are declared using the enum keyword.
The general syntax looks like this:
enum enumName {
value1,
value2,
value3,
...
};
You can then declare variables of that enum type and assign values.
Example:
#include <iostream>
using namespace std;
enum Color { Red, Green, Blue };
int main() {
Color c1 = Red;
cout << "Color value: " << c1;
return 0;
}
Output:
Color value: 0
Even though you see Red, the program actually stores it as the integer 0.
Assigning Custom Values in Enums
You can assign custom integer values to any enumerator.
Subsequent values increase automatically unless explicitly defined.
Example:
#include <iostream>
using namespace std;
enum Status { Pending = 1, Approved = 3, Rejected = 5 };
int main() {
Status s = Approved;
cout << "Status: " << s;
return 0;
}
Output:
Status: 3
If you assign one value manually, the next enumerators continue from that number unless given another specific value.
Using Enums in Conditional Statements
Enums work well with conditional logic like if, switch, and for statements.
Example: Enum with Switch Case
#include <iostream>
using namespace std;
enum Direction { North, South, East, West };
int main() {
Direction dir = East;
switch (dir) {
case North:
cout << "Moving North";
break;
case South:
cout << "Moving South";
break;
case East:
cout << "Moving East";
break;
case West:
cout << "Moving West";
break;
}
return 0;
}
Output:
Moving East
This makes programs more readable because you use meaningful names instead of raw numbers.
Enum Variable Assignment and Comparison
You can assign and compare enum variables directly since they represent integers internally.
Example:
enum Level { Low, Medium, High };
int main() {
Level l1 = Medium;
Level l2 = High;
if (l2 > l1)
cout << "High level is greater than Medium.";
return 0;
}
Output:
High level is greater than Medium.
Here, Medium is internally 1 and High is 2, so the comparison works numerically.
Enum Scope and Type Safety
By default, traditional enums in C++ are not strongly typed.
That means you can mix them with integers, which may lead to unexpected results in large programs.
Example of potential issue:
enum Fruit { Apple, Mango };
int x = Apple;
x = 10; // Works, but not ideal
To solve this, C++11 introduced scoped enums (also known as enum class).
Enum Class (Scoped Enum) in C++
Scoped enums provide strong typing and better control.
You must use the enum name to access its members, avoiding naming conflicts.
Example:
#include <iostream>
using namespace std;
enum class TrafficLight { Red, Yellow, Green };
int main() {
TrafficLight light = TrafficLight::Green;
if (light == TrafficLight::Green)
cout << "Go!";
else
cout << "Stop!";
return 0;
}
Output:
Go!
Key Differences:
| Feature |
Traditional Enum |
Enum Class |
| Type Safety |
Weak (can mix with int) |
Strong (cannot mix with int) |
| Scope |
Global |
Scoped |
| Syntax |
Red |
TrafficLight::Red |
| Implicit Conversion |
Allowed |
Not allowed |
Changing Underlying Type of Enum Class
By default, enums are stored as int. But you can specify another underlying type using a colon (:).
Example:
enum class ErrorCode : char { Success = 'S', Failure = 'F' };
This gives more flexibility when dealing with memory optimization or special representations.
Enum in Loops
Enums can be used with loops to iterate through a sequence of values.
Example:
#include <iostream>
using namespace std;
enum Weekday { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
int main() {
for (int i = Sun; i <= Sat; i++) {
cout << i << " ";
}
return 0;
}
Output:
0 1 2 3 4 5 6
Although enums store names, they behave like integers in such cases.
Input and Output with Enums
By default, enums cannot directly take input as strings or display their names as output.
You can handle this by using arrays or switch statements to map between strings and enum values.
Example:
#include <iostream>
using namespace std;
enum Month { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };
int main() {
int num;
cout << "Enter month number (0–11): ";
cin >> num;
Month m = static_cast<Month>(num);
switch (m) {
case Jan: cout << "January"; break;
case Feb: cout << "February"; break;
case Mar: cout << "March"; break;
default: cout << "Other month"; break;
}
return 0;
}
Output:
Enter month number (0–11): 2
March
Advantages of Using Enums in C++
-
Improves readability – names are easier to understand than numbers.
-
Organizes constants in one place.
-
Prevents invalid values (only defined constants can be used).
-
Easier debugging since you know what each constant represents.
-
Scoped enums add strong type safety in modern C++.
Summary of C++ Enums
An enum in C++ is a convenient way to group related named constants.
It replaces hard-coded integers with readable identifiers.
You can use traditional enums for simple tasks and enum class for type-safe programming.
Enums improve code organization, readability, and maintainability — especially in larger projects.