C++ Enumeration2 min read

An enumeration is a user-defined type that consists of a set of named integral constants. These constants are fixed constant. The keyword enum is used to define these data types.

Syntax of enums

enum enum_name{const1, const2, ....... };

  • enum_name: Name of the enum given by the user.
  • const1, const2 : These are the value of type enum_name.

Let us see an example to define enum.

enum season { spring, summer, autumn, winter };

The above is the enum for the season, spring, summer, autumn and winter are the types of the season and by default, the value starts in increasing order from zero such as spring is 0, summer is 1, autumn is 2 and winter is 3.

We can change the default values of enum and assign the required value in the following way.

enum season { spring = 01, summer = 04, autumn = 06, winter=09 };


How to declare an enum variable

We can declare an enum variable in two different ways:

  1. We can declare it at the time of defining enum, just add the enum variable name at the end and before the semicolon.
  2. Another way is we can create it inside the main function. Both ways are shown below.

Example of enumeration in c++

1. Let us see an example of enum in c++ with the first way of declaring variables.

Output:

//Output
Day: 2


2. Another example of enum, where we declare the variable inside the main function. Also, we change the default values of enums.

Output:

//Output
Green: 24


Why use enum in C++

We use enum when we want to assign the variable with one of the possible sets of values. These values cannot be changed, if we try to assign different values to this variable the compiler generates an error. Thus, it improves safety and increases compile-time checking.

There is another use of enum that is in a switch case statement. Also, enum can be traversed.


MORE

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …