Enumerations In C

An enumeration is a data type similar to a structure or a union. the enumerated data type is also the user – defined data type which provides a way for attaching names to numbers, thereby increasing the comprehensibility of the code. These data type work when we know in advance a finite list of values that a data type can take on. Its members are constants that are written as identifiers, through they have signed integer value. These constants represent values that can be assigned to corresponding enumeration variable. the general format for declaration is:

enum tag {member1, member2, member3, . . . .membern}

where enum is a required keyword tag is a name that identifies enumeration and member1, member2, member3, . . . . . . membern are individual identifiers. The member names must distinct from other member names.

Once the enumeration has been defined, corresponding enumeration variables can be declared as

storage – class enum tag var1, var2, var3, . . . . . .varn;

where the storage – class is an optional storage class specifier.

For example:

enum week _ days {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};

to create variable of week – days type, we can write

enum week _ days day1, day2;

In an enumeration, each symbol stands from an integer value. The enumeration constants will represent the following integer values.

Sunday          0
Monday          1
Tuesday         2
Wednesday       3
Thursday        4
Friday          5
Saturday        6

Program – Use of enum

#include<stdio.h>
void main ()
{
     enum week_days {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, 
         Saturday};
     enum week _ days day1;
     day1 = Sunday;
     if (day == 0)
            printf (“Sunday”);
}

Output

Sunday



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext