Skip to main content

Command Palette

Search for a command to run...

Enumeration

Published
3 min readView as Markdown

🟢 Step 1: What is an Enum?

👉 Enum (short for Enumeration) means

giving names to numbers — so your code becomes easy to read and understand.

It’s a user-defined data type that allows you to assign names to integer values.


🔸 Without enum (normal way):

int day = 3;  // 3 means Wednesday

Can anyone guess what “3” means here? 🤔
No! It could be any day — hard to understand.


🔹 With enum:

enum Week { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };

Now you can write:

Week today = Wednesday;

Now it’s clear that today is Wednesday — not just a random number.


🔹 Internally how it works:

C++ automatically gives numbers (starting from 0):

NameValue
Monday0
Tuesday1
Wednesday2
Thursday3
Friday4
Saturday5
Sunday6

🟢 Step 2: A simple example

#include <iostream>
using namespace std;

enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };

int main() {
    Days today = Monday;

    if (today == Sunday) {
        cout << "Today is Sunday — it's a holiday!" << endl;
    } else {
        cout << "Today is a working day!" << endl;
    }

    return 0;
}

🧠 Output:

Today is a working day!

🟢 Step 3: What happens here

  • enum creates a list of names (Sunday, Monday, …)

  • C++ replaces them with numbers (0, 1, 2, …)

  • You can use the names instead of numbers → code becomes readable and meaningful


🟢 Step 4: Why do we use enum?

ProblemEnum Fixes It
Numbers are confusingNames are clear
Hard to remember what 0 or 1 meansEasy to remember names
Code looks messyCode looks neat and readable

🟢 Step 5: Real-life Example — Traffic Light 🚦

#include <iostream>
using namespace std;

enum Light { Red, Yellow, Green };

int main() {
    Light signal = Red;

    if (signal == Red)
        cout << "STOP!" << endl;
    else if (signal == Yellow)
        cout << "READY!" << endl;
    else
        cout << "GO!" << endl;

    return 0;
}

🧠 Output:

STOP!

🟩 In one simple line:

“Enum is a list of names that represent numbers — making the code easier to understand.”

Example:

enum Days { Sunday, Monday, Tuesday };

Summary:

FeatureDescription
TypeUser-defined
Starts from0 by default
PurposeReplaces confusing numbers with readable names
Exampleenum Color { Red, Green, Blue };
UseMakes code simple and meaningful

More from this blog

Amit singh's blog

235 posts