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

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 …
Read More

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More