C - Enumeration (or enum)

Enumeration (or enum) is a user defined data type in C. An enumerated data type provides a way for attaching names to numbers, the names make a program easy to read and maintain. Enum automatically enumerates a list of words by assigning values 0,1,2,3 and so on.

Syntax: Enum declaration.

Enum height (large, medium, small);
Enum colour ( black, white);

Example 1: Weekdays is defined as numeric value using enum.

enum week{Sun, Mon, Tue, Wed, Thur, Fri, Sat}; 
  
void main() 
{ 
    enum week day;
    day = Wed;
    printf("Day = %d",day);
    getch();
}
Day = 3

Example 2: Assign month name with numeric value.

enum year{Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec}; 
  
void main() 
{ 
   int i; 
   for (i=Jan; i<=Dec; i++)       
      printf("%d ", i); 
   getch();
}
0 1 2 3 4 5 6 7 8 9 10 11

Example 3: We can assign values to some name in any order.

enum day {sun = 1, mon, tue = 5, wed, thu = 10, fri, sat}; 
  
void main() 
{ 
    printf("%d %d %d %d %d %d %d", sun, mon, tue, wed, thu, fri, sat); 
    getch();
} 
1 2 5 6 10 11 12