Enumeration in C++

What is Enumeration ?

Enumeration in C++, helps the programmer to create user defined data-types, to make the code more easy and readable. Enum saves time and makes the program more readable. To define enums, the enum keyword is used and enum variables can also take their corresponding integral value, starting from zero.


Enumeration used in C++

Enum is almost similar to structure, as it can hold any data-type, which the programmer may want, this is advantageous when the program gets complicated or when more than one programmer would be working on it, for the sake of better understanding.


Declaring a enum is almost similar to declare a structure.
We can declare a enum using the keyword “enum” followed by the enum name, and than stating its elements with the curly braces “{}”, separated by commas.

enum color {
  • The first part of the above declaration, declares the data type and specifies its possible values. These values are called “enumerators”.
  • The second part declares variables of this data-types.

Working of Enumeration

The variables declared within the enum, are treated as integers, i.e. each variable of the enumerator can take its corresponding integral value, starting from zero(0). That means in our above discussed example,

  • red = 0
  • green = 1
  • blue = 2

The main purpose of using enumerated variables is to ease the understanding of the program. For example, if we need to employee departments in a payroll program, it’ll make the listing easier to read if we use values like Assembly, Production, Accounts, etc, rather than using 1,2 and 3.

Let’s take one more example for understanding this:


#include <bits/stdc++.h> 
using namespace std;

int main()
       enum emp_dept    //declaring enum emp_dept and its variables
       struct employee   //declaring srtucture
              char name[30];
              int age;
              float bs;
              enum emp_dept department;
         struct employee e;
       strcpy(e.name, "Nitin Kumar");   //intializing employee details
       printf("\n Name = %s",e.name);
       printf("\n Age = %d",e.age);
       printf("\n Basic Salary = %f",e.bs);  
       printf("\n Department = %d",e.department);

              printf("\n %s is an accountant",e.name);
              printf("\n %s is not an accountant",e.name);
    return 0;


 Name = Nitin Kumar
 Age = 22
 Basic Salary = 12500.000000
 Department = 3
 Nitin Kumar is not an accountant

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription