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.

Syntax
enum EnumName {
    constant1,
    constant2,
    constant3
};

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;
    }
}

0 comments

Post a Comment