C Programming

Enumerators in C

In C language, there are two types of data types present. They are predefined and user-defined data types. Predefined data types are integer, character, float, etc. They are also called primitive data types. User-defined data types consist of a collection of predefined data types. It is Meta information.

Similar in structure, a union enumerator is also a user-defined data type. In C language, it is represented as an enum keyword.

With the help of an enumerator, we can create our data type and data.

Define an Enumerator

enum month
{
Jan, Feb, Mar, Apr, … Dec
};

Here, the month is a user-defined data type, and we cannot declare any variable. These variables are not variables. These are the possible values in the month data type. Possible values mean a year contains 12 months, like January, February, and March. They are one kind of value.

Creating Variables of Enumerators

enum month
{
Jan, Feb, … Dec
};
main ()
{
enum month m1, m2, m3;
}

Explanation

Here, possible values are stored, which are written inside the month data type. We can directly use the name of a month despite any number.

From the perspective of a compiler, the enumerator acted like an integer.

All possible values inside the enumerator behaved like an integer. By default, its value is started with 0. In the example, Jan is stored as 0, Feb is stored as 1… Dec is stored as 11.

Changing the Default Value of enum Elements

enum month
{
Jan= 1, Feb, Mar, Apr= 9, May… Dec
}

We can initialize enumerators with a different integer value.

Another Example

enum boolean // It increases the readability of a program.
{
false, true
};

We can write any program in the C language without the help of enumeration, but enumeration helps in writing clear codes and simplifying the program.

Programming Example 1

Here, we see an application of an enumerator in the next programming example:

#include <stdio.h>
enum fruits {mango = 1, apple, banana = 5,
        orange, strawberry = 10, grape, pear} ; // defining an enumerator.

int main()
{
    printf ( " %d %d %d %d %d %d %d \n ", mango, apple, banana,
            orange, strawberry, grape, pear ) ; // print the values.
    return 0 ;
}

Output

Explanation

Here, we define an enumerator named fruits, and it has some elements. The elements are mango, apple, banana, orange, strawberry, grape, and pear. We set a value to the element mango as 1. So, the value of the next element, apple, must be 2. Again, we set a value of element strawberry as 10. So, the next element, strawberry, must be 12. We can solve the problem using enumerators.

Programming Example 2

Here, we see another example of an enumerator. How does the enumerator work on a program?

#include <stdio.h>
enum mnt
{Jan, Feb, Mar, Apr, May, Jun, Jul,
 Aug, Sep, Oct, Nov, Dec} ; // definition of an enumerator.

int main()
{

enum mnt mon = June ;

    printf ( " Value of June in month: %d ",  mon ) ;
return 0 ;

}

 

Output

Explanation

Here, we define an enumerator called month. Inside the enumerator, there are 12 elements. These elements are all the months of a year, like January, February, March etc. Now, inside the main() function, we create a variable of month data type, mon, and assign a value june. After that, we print the value of mon. It shows the output 5, as the first element of the enumerator, starts with a default value of zero, and June is the sixth element of this enumerator. For this, the result is shown as 5 here.

Programming Example 3

Here, we define several more examples of the enumerator:

#include <stdio.h>
enum weekdays{ sun, mon, tue, wed, thurs, fri, sat} ;   // defining an enumerator.
Int main()
{
    enum  weekdays d =  tue ;       // create a variable of this enumerator.
       
    Printf ( “OUTPUT IS: %d\n “, d+2 ) ;
}

Output

Explanation

Here, we define an enumerator called weekdays. Inside the enumerator, there are seven elements. These elements are all the days of a month, like Monday, Tuesday, Wednesday, etc. Inside the main() function, we create a variable of weekdays data type, d, and assign a value, tue. After that, we print the value of d+2. It shows the output 4 as the first element of enumerator starts with default value zero, and tue is the 3rd element of this enumerator. For this, the result is 4 here as we sum 2 with the value of tue.

Programming Example 4

Now, we will see more examples of an enumerator.

#include <stdio.h>
enum weekdays{ sun, mon, tue, wed, thurs, fri, sat} ;
int main()
{
    int i ;
   for ( i =  sun;  i <= sat ;  i++ )      
      printf ( " %d  ",  i ) ;
return 0 ;  
}

Output

Explanation

Here, we define an enumerator called weekdays. Inside the enumerator, there are seven elements. These elements are all the days of a week, like Monday, Tuesday, Wednesday, etc. Now, inside the main() function, we apply for loop and assign the value of control variable i = sun. After that, we print the value of i. It shows the output of all the values of the elements, as the 1st element of the enumerator start at a default value of zero.

Programming Example 5

In this programming example, we will see the last example of the enumerator.

#include <stdio.h>
enum week {
  monday,
  tuesday,
  wednesday,
  thursday,
  friday,
  saturday,
  sunday
} ;     // defining an enumerator.

int main( void ) {
enum  Week day = Tuesday ;      // declaring and assigning a value.

  if ( day == monday ) {
    printf  ( " Today is monday \n " ) ;
  } else {
    printf ( " Today is  not monday \n " ) ;
  }
return 0 ;
}

Output

Explanation

Here, we define an enumerator called week. Inside the enumerator there are seven elements. These elements are all the days of a month, like Monday, Tuesday, Wednesday etc. Inside the main() function, we create a variable of the week as a day and assign a value to Tuesday.

Conclusion

Enumerator is a very useful concept in terms of the C language. After defining all the aspects of an enumerator, we have come to the conclusion that an enumerator helps us manage our data and values easily. It enhances the readability of the program and makes the program easier.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com