The Wayback Machine - https://web.archive.org/web/20240118170025/https://www.geeksforgeeks.org/enumeration-in-cpp/
Open In App
Related Articles

Enumeration in C++

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Enumeration (Enumerated type) is a user-defined data type that can be assigned some limited values. These values are defined by the programmer at the time of declaring the enumerated type.

If we assign a float value to a character value, then the compiler generates an error. In the same way, if we try to assign any other value to the enumerated data types, the compiler generates an error. Enumerator types of values are also known as enumerators. It is also assigned by zero the same as the array. It can also be used with switch statements.

Syntax:

enum enumerated-type-name
{
    value1, value2, value3â€Ķ..valueN
};

For Example: If a gender variable is created with the value male or female. If any other value is assigned other than male or female then it is not appropriate. In this situation, one can declare the enumerated type in which only male and female values are assigned.

Enum in C++

The enum keyword is used to declare enumerated types after that enumerated type name was written then under curly brackets possible values are defined. After defining Enumerated type variables are created. 
Enumerators can be created in two types:-

  1. It can be declared during declaring enumerated types, just add the name of the variable before the semicolon. or,
  2. Besides this, we can create enumerated type variables as the same as the normal variables.
enumerated-type-name variable-name = value;

By default, the starting code value of the first element of the enum is 0 (as in the case of the array). But it can be changed explicitly.

Example:

enum enumerated-type-name{value1=1, value2, value3};

Also, The consecutive values of the enum will have the next set of code value(s).

Example:

//first_enum is the enumerated-type-name
enum first_enum{value1=1, value2=10, value3};

In this case, 
first_enum e;
e=value3;
cout<<e;

Output:
11

Example:

C++




// C++ Program to Demonstrate the Functioning of Enumerators
// with an example of Gender
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // Defining enum Gender
    enum Gender { Male, Female };
  
    // Creating Gender type variable
    Gender gender = Male;
  
    switch (gender) {
    case Male:
        cout << "Gender is Male";
        break;
    case Female:
        cout << "Gender is Female";
        break;
    default:
        cout << "Value can be Male or Female";
    }
    return 0;
}


Output: 

Gender is Male

 

Example:

C++




// C++ Program to Demonstrate the Functioning of Enumerators
// with an Example of Year
#include <bits/stdc++.h>
using namespace std;
  
// Defining enum Year
enum year {
    Jan,
    Feb,
    Mar,
    Apr,
    May,
    Jun,
    Jul,
    Aug,
    Sep,
    Oct,
    Nov,
    Dec
};
  
// Driver Code
int main()
{
    int i;
  
    // Traversing the year enum
    for (i = Jan; i <= Dec; i++)
        cout << i << " ";
  
    return 0;
}


Output: 

0 1 2 3 4 5 6 7 8 9 10 11

 


Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 05 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments