Thursday, 19 February 2026

Enumerations in C++

Enumerations in C++ (enum)
An enumeration (enum) is a user-defined data type that assigns names to integer constants, making programs more readable and structured. An enum is a symbolic name for a set of integer values.

Enumeration data types are available in other high level programming languages such as Pascal and Ada. It is also supported by all C++ compilers. An enumeration data type is a set of values represented by identifiers called enumeration constants. The enumeration constants are specified when the type is defined. The general format of the enumeration data type is,
Syntax
enum user_de ned_name {
member 1;
member 2;
------
------
member n;
};

where enum is a keyword for defining the enumeration data type and the braces are essential. The members of the enumeration data type such as member 1, member 2 and member n are the individual identifiers. Once the enumeration data type is defined, it can be declared in the following ways:
storage_class enum user_de ned_name variable1,variable2 ..variable

where the storage class is optional. For example, following are valid enumeration data type declarations:
(1)
enum sample {
mon,tue,wed,thu,fri,sat,sun };
enum sample day1,day2,day3;
(2)
enum drinks {
cola,maza,limca,rasna };
enum drinks ravi,raju,rani;
(3)
enum games {
tennis,chess,shuttle,swimming,walking };
enum games student,staff;
The enumeration data type declaration can be written in a single declaration as:
enum sample {
mon,tue,wed,thu,fri,sat,sun } day1,day2,day3;
which is exactly equivalent to
(1)
enum sample {
mon,tue,wed,thu,fri,sat,sun } day1;
enum sample day2,day3;
(2)
enum sample {
mon,tue,wed,thu,fri,sat,sun };
enum sample day1;
enum sample day2;
enum sample day3;
The enumeration constants can be assigned to the variable like
day1 = mon;
day2 = tue and so on.
Enumeration constants are automatically assigned to integers starting from 0, 1, 2 etc. up to the last number in the enumeration.

Example
#include <iostream>
using namespace std;
enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
int main() {
    Day today = Wed;
    cout << today;   // Output: 3
}

Assigning Custom Values
enum Status {
    Success = 1,
    Error = 5,
    Pending = 10
};
You can also mix:
enum Numbers { A = 10, B, C };  
// B = 11, C = 12

Size of Enum
  • Usually same as int (4 bytes)
  • Depends on compiler
Enum Example with Switch
#include <iostream>
using namespace std;
enum Color { Red, Green, Blue };
int main() {
    Color c = Green;
    switch(c) {
        case Red: cout << "Red"; break;
        case Green: cout << "Green"; break;
        case Blue: cout << "Blue"; break;
    }
}

Example:-
// enumeration 1.cpp
#include <iostream>
using namespace std;
int main ()
{
enum sample {
mon,tue,wed,thu,fri,sat,sun }
day1,day2,day3,day4,day5,day6,day7;
day1 = mon;
day2 = tue;
day3 = wed;
day4 = thu;
day5 = fri;
day6 = sat;
day7 = sun;
cout << “ Monday = ” << day1 << endl;
cout << “ Tuesaday = ” << day2 << endl;
cout << “ Wednesday = ” << day3 << endl;
cout << “ Thursday = ” << day4 << endl;
cout << “ Friday = ” << day5 << endl;
cout << “ Saturday = ” << day6 << endl;
cout << “ Sunday = ” << day7 << endl;
return 0;
}
Output of the above program
Monday = 0
Tuesday = 1
Wednesday = 2
Thursday = 3
Friday = 4
Saturday = 5
Sunday = 6

These integers are normally chosen automatically but they can also be specified by the programmer. For example,
enum sample {
mon,tue,wed = 10,thu,fri,sat = 15,sun }
day1,day2,day3,day4,day5,day6,day7;
The C++ compiler assigns the enumeration constants as
Monday = 0
Tuesday = 1
Wednesday = 10
Thursday = 11
Friday = 12
Saturday = 15
Sunday = 16

Even negative integers are permitted to be defined as enumeration constants. For example, if,
enum sample {
mon, tue, wed = 10,thu,fri = -1,sat,sun }
day1,day2,day3,day4,day5,day6,day7;
then the C++ compiler assigns the following enumeration constants:
Monday = 0
Tuesday = 1
Wednesday = 10
Thursday = 11
Friday = -1
Saturday = 0
Sunday = 1

0 comments

Post a Comment