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

In the above enum syntax code we have declared or created an enum, starting with enum keyword and followed by the name of the enum, which in this case is level. After that we have opening and closing curly brackets with in which we have three enum items LOW, MEDIUM, and HIGH, which are separated with commas at the end of each enum item, just don't put comma after the last enum item which is HIGH. And the starting value of these enum items is 0 (zero) for enum item LOW. And at last remember to put semi-colon(;) at the end of the closing curly bracket like this,
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

Now look at the third C++ enum coding example, we have created an enum and named it as Level. It contains three enum items which are, LOW, MEDIUM, and HIGH. And if we notice that all the three enum items are assigned a constant value i.e, LOW = 5, MEDIUM = 10, and HIGH = 15 like this,
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

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

Now in the second enum coding we have same enum Level with three items in it. But this time we are going to use switch statement like this,

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

Popular posts from this blog

How to easily Download and Install Microsoft visual studio community 2022 for free

For Loop In C++ Programming Language

First C Program. How to write Hello World Program In C Programming Language?

Functions In C++ Programming Language

Nested Loops In C++ Programming Language