What is an Enum in C++? Syntax, Examples, and Best Practices
What is an Enum in C++? Syntax, Examples, and Best Practices
Last updated on : November 11, 2026
In todays blog post we are going to cover the concept of Enums or Enumeration in C++ programming language. Enum in C++ program is basically a user-deined data type. With the help of enums we can assign meaningful names to a set of integer constants. We can simply replace raw numbers with readable labels to make code clearer and less prone to bugs. And if we talk about some practical implementation of enums in C++ program, with the help of enums we can represent values like.
1) Days of the week.
2) Months of the year.
3) Directions.
4) States.
5) And other predefined groups of constants.
So basically enums can help us when we have a little set of possible values for an item like directions, days of week, etc. And remember an important point that we should define enum first before we can create its variables.
There is another important point to keep in mind, the starting value of enum item is zero (0) by default. And each enum item which is also referred as enumerator they automatically increases by 1 (one) unless we explicitly assigned some different value to enum items or its enumerators.
Some basic rules to create enums in C++ programming language
Now remember in order to create an enum, we use enum keyword followed by the name of the enum as per our program logic. After creating an enum then we can write or insert enum items in it with in the curly brackets and seprate them with commas. And remember that these enum items or enumerators have starting value 0 by default and increment by 1 with each next enum item in the list. And with in the curly brackets each enum item should have a unique name with in an enum. And remember to put semi-colon (;) at the end of closing curly bracket.
Basic syntax and working of Enum in C++ programming language
Syntax code example 1
enum level { LOW, MEDIUM, HIGH };
void main() { enum level enumvar = MEDIUM; }
Explanation of syntax 1
enum level { LOW, MEDIUM, HIGH };
Now with in the main() method we have declared and initialized an enum variable and named it as enumvar, after that we have assigned it a enum item MEDIUM like this,
void main() { enum level enumvar = MEDIUM; }
Till now we have created an enum, and declared an enum variable inside main() method, and after that we can simply print the enum variable value to the console screen using the cout<< statement in C++ programming language.
How to change default values of enum items in C++ programming language
First enum code
enum Level { LOW, MEDIUM, HIGH };
Explanation of first code
We have talked about enums and its items or enumerators, and till now we have sutdied that the starting value of enum first item is 0 (zero) by default. But now we will look at some ways with the help of which we can assign different values to the enum items.
In the first C++ enum code we have three enum items LOW, MEDIUM, and HIGH. And we did not assign any value to the enum items. For such enums the starting value of enum item no 1 (LOW) will be 0 (zero) which is the default value.
Second enum code
enum Level { LOW = 1, MEDIUM, HIGH };
Explanation of second code
Now look at the second C++ enum coding example we have created an enum named as Level and it also contains three enum items which are LOW, MEDIUM, and HIGH. But here we can notice that the first enum item which is LOW is assigned a value of 1, which means that now the starting value of enum items is not zero (0), now the starting value of enum item no 1 which is LOW is equal to 1.
Third enum code
enum Level { LOW = 5, MEDIUM = 10, HIGH = 15 };
Explanation of third code
LOW = 5, MEDIUM = 10, HIGH = 15
So in this C++ enum coding example we have assigned different values to each enum item.
Simple coding examples of enums in C++ programming language
C++ code example 1
#include<iostream> #include<conio.h> using namespace std; enum level { LOW, MEDIUM, HIGH }; void main() { enum level enumvar = MEDIUM; cout << enumvar << endl; _getch(); }
Explanation of code 1
Now we are going to look at practical C++ coding examples with respect to enums in C++ programming language. In the first coding example we have created an enum and named it as Level. with in this enum we have items LOW, MEDIUM, and HIGH like this,
enum level { LOW, MEDIUM, HIGH };
And the starting value for enum item no 1 (LOW) will be 0 (zero). In the main() method we have declared and initialized an enum variable, which is enumvar, and assigned it a value of enum item LOW like this,
enum level enumvar = MEDIUM;
And after that we have simply printed the value of enumvar to the console screen like this,
cout << enumvar << endl;
Output of code 1
C++ code example 2
#include<iostream> #include<conio.h> using namespace std; enum level { LOW, MEDIUM, HIGH }; void main() { enum level enumvar = HIGH; switch (enumvar) { case 0: { cout << "Low Level"; break; } case 1: { cout << "Medium Level"; break; } case 2: { cout << "High Level"; break; } } _getch(); }
Explanation of code 2
enum level enumvar = HIGH; switch (enumvar)
and with in that switch statement we will use enumvar, with in the opening and closing round brackets like this,
switch (enumvar)As we know that enumvar contains some integer constant value which we assigned to it during declaration and initialization like this,
enum level enumvar = HIGH;
After that we will make cases of switch statement with respect to enumvar constant integer values like this,
switch (enumvar) { case 0: { cout << "Low Level"; break; } case 1: { cout << "Medium Level"; break; } case 2: { cout << "High Level"; break; } }
As we know that in this C++ coding example the value inside enumvar is equal to 2 and thus it will run the case no 2 which is,
case 2: { cout << "High Level"; break; }
And it will simply print the message High Level to the console screen.
So this the basic implementation of enums with switch statement in C++ programming language.
Output of code 2
High Level
Congratulations! like always we have successfully implemented enums in C++ programming language.
Comments
Post a Comment