The Beginners Guide to Enumeration in C

Filed Under: C Programming
Enumeration In C (2)

Hey, folks! Hope you all are doing well. In this article, we will be discussing Enumeration in C .


What is Enumeration?

Enumeration in C is a user-defined data type. Thus, we can customize the data and store it into it according to the needs of the user/system.

Enumeration assigns string or character values i.e. enumeration tags to the integer data values. For every string literal or value, there is an integer index assigned to it as seen below.

Enumeration In C
Enumeration In C

Like a C array, enum starts the index values from 0.

Thus, enum helps us to simplify the code and increase the readability of the program.

Now, let us understand the structure of Enumeration in C programming.


Syntax of Enumeration in C

So, now let us have a look at the declaration of Enum in C:

enum enum-name {enumerators};
  • enum-name: A valid name will be referred for all the enumeration related tasks.
  • enumerators: The named integer literals.

In order to fetch and display the data stored in enum, we need to associate an object to it using the below command:

enum enum-name object;

Further, to display any data value stored in the enum, we can associate the object to the specified string-literal as shown below:

object = enumerator;

Let us now implement the above syntax of enumeration through some examples.


Examples Usage of Enum in C

In the below example, we have stored the names of the months in the enum and accessed the value of ‘Dec’ using object assigned to ‘year’.

#include<stdio.h> 

enum year{Jan, Feb, Mar, Apr, May, Jun, July, Aug, Sep, Oct, Nov, Dec}; 

void main() 
{ 
	enum year obj; 
	obj = Dec; 
	printf("The value of Dec:\n%d",obj); 
}

Output:

The value of Dec:
11

Now, we have created an enum of the week days and accessed every value assigned to it using a for loop.

#include<stdio.h> 

enum Week_day{Mon, Tue, Wed, Thur, Fri, Sat, Sun};

int main()
{
    for (int x=Mon; x<=Sun; x++)       
        printf("Day: %d\n", x); 
    return 0;
}

Output:

Day: 0                                  
Day: 1
Day: 2
Day: 3
Day: 4
Day: 5
Day: 6                                                                                                                        

In enumeration, if we assign a particular integer value to some names or literals, all the rest of the un-assigned names would have a value which would be the value of the previous name plus 1.

Have a look at the below example for the same.

#include<stdio.h> 

enum Week_day{Mon=10, Tue, Wed, Thur, Fri, Sat, Sun}; 
int main()
{
    
    for (int x=Mon; x<=Sun; x++)       
      printf("Month: %d\n", x); 
    return 0;
}

We had assigned ‘Mon’ = 10, further to which all the rest of the enum literals got their values as the value of the previous name plus 1.

Output:

Month: 10
Month: 11
Month: 12
Month: 13
Month: 14
Month: 15
Month: 16

Multiple names/string literals in enumeration can have similar value or same values assigned to them.

Let us have a look at the below example to validate the above statement.

#include <stdio.h> 
enum switch_value {YES = 1, NO = 0, NEVER = 0}; 

int main() 
{ 
    printf("YES: %d\t, NO: %d\t, NEVER: %d", YES, NO, NEVER); 
    return 0; 
}

Output:

YES: 1	, NO: 0	, NEVER: 0

Conclusion

By this we have come to the end of this topic. Please feel free to comment below, in case you come across any doubt. Till then, Happy Learning!


References

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages