Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

C programming – Enumeration (enum)

An enumeration is a user defined data type.

Syntax

enum <tagname>{ member1, member2, member3, ----, memberN };

C Programming Language Enum Syntax

Example

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

Why Enumeration?

Enumeration allows the programmer to assign names to integer constants.
Naming to integer constants makes a program easy to read & operate.

Let’s consider an enum given below:

enum planetOrder { Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune};

We all know that
Mercury is the 1st planet from the Sun, Venus is a 2nd, Earth is a 3rd and so on.

With the help of enum, instead of using position numbers of planets, we can use their names directly in a program.

But according to the rule of the enum, by default every starting member is initialized with zero and rest of the members of the enum is initialized with increment of one, sequentially.

Thus Mercury will be initialized with 0, Venus with 1, Earth with 2 and so on.

Example

#include <stdio.h>

enum planetOrder { Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune};

int main()
{
    printf("\n Mercury = %d",Mercury);
    printf("\n Venus = %d",Venus);
    printf("\n Earth = %d",Earth);
    printf("\n Mars = %d",Mars);
    printf("\n Jupiter = %d",Jupiter);
    printf("\n Saturn = %d",Saturn);
    printf("\n Uranus = %d",Uranus);
    printf("\n Neptune = %d",Neptune);
    return 0;
}

Output

Mercury = 0 
Venus = 1 
Earth = 2 
Mars = 3 
Jupiter = 4 
Saturn = 5 
Uranus = 6 
Neptune = 7

We can make starting member of enum initialized with any integer value other than zero explicitly, as shown below:

Example

#include <stdio.h>

enum planetOrder { Mercury=1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune};

int main()
{
    printf("\n Mercury = %d",Mercury);
    printf("\n Venus = %d",Venus);
    printf("\n Earth = %d",Earth);
    printf("\n Mars = %d",Mars);
    printf("\n Jupiter = %d",Jupiter);
    printf("\n Saturn = %d",Saturn);
    printf("\n Uranus = %d",Uranus);
    printf("\n Neptune = %d",Neptune);
    return 0;
}

Output

Mercury = 1
Venus = 2
Earth = 3
Mars = 4
Jupiter = 5
Saturn = 6
Uranus = 7
Neptune = 8

Now in the above program, we can see that Mercury is initialized with 1 & rest of the members is initialized with an increment of one by default, sequentially.

It is also possible to assign any integer value to any enum member, directly.
The successive enum member that is not assigned will be initialized with a value one plus than the previous member.

Example

#include <stdio.h>

enum planetOrder {Mercury=1, Venus, Earth, Mars=10, Jupiter, Saturn, Uranus, Neptune};

int main()
{
    printf("\n Mercury = %d",Mercury);
    printf("\n Venus = %d",Venus);
    printf("\n Earth = %d",Earth);
    printf("\n Mars = %d",Mars);
    printf("\n Jupiter = %d",Jupiter);
    printf("\n Saturn = %d",Saturn);
    printf("\n Uranus = %d",Uranus);
    printf("\n Neptune = %d",Neptune);
    return 0;
}

Output

Mercury = 1
Venus = 2
Earth = 3
Mars = 10
Jupiter = 11
Saturn = 12
Uranus = 13
Neptune = 14

Enum with switch case

Example

#include <stdio.h>

enum WeekDays { Sunday=1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};

int main()
{
    enum WeekDays wd;
    int num;
    printf("\n Enter any number :: ");
    scanf("%d",&num);
    wd = num;
    switch(wd)
    {
        case Sunday:
                    printf("\n Sunday is the 1st day of the week");
                    break;
        case Monday:
                    printf("\n Monday is the 2nd day of the week");
                    break;
        case Tuesday:
                    printf("\n Tuesday is the 3rd day of the week");
                    break;	
        case Wednesday:
                    printf("\n Wednesday is the 4th day of the week");
                    break;	
        case Thursday:
                    printf("\n Thursday is the 5th day of the week");
                    break;	
        case Friday:
                    printf("\n Friday is the 6th day of the week");
                    break;	
        case Saturday:
                    printf("\n Saturday is the 7th day of the week");
                    break;	
        default:
                    printf("\n Invalid entry");
                    break;								
    }
    return 0;
}

Output

Enter any number :: 4

Wednesday is the 4th day of the week

 

Leave a reply