Enum in C

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
By Prateek Narang
Free
star5
Enrolled: 1000
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
Prateek Narang
Free
5
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Overview

In computer programming enumerated data type is used to create the group of constants. This group of named values can be identified as elements, members, enumerals, or enumerators. This enumerators are considered very handy for designing a big scale applications.

Scope

  • This article starts at very basic level by creating a problem to establish a use case for enum.
  • Then it explains the enum and introduces you with different ways to declare and initialize the enums.
  • This article will help you to encounter the possible scenarios and use cases where you can use the enums in your code.

Introduction

Imagine a scenario where we are designing a text editor and want to have a features like bold, italic and underline.

Now what are different ways with which you can design and access them in your program?

One way is to use string literals like "BOLD", "ITALIC" or "UNDERLINE", but the problem arises when you want to use them in a switch/case statements. It becomes complicated.

Another way is to map them with certain numbers like 0, 1 or 2, but having string "BOLD" is more meaningful in code instead of having any random number 0.

So we want the string literals, but we want them to work as integer constants.

There is one classic way to solve this problem and achieve what we want by using using macros in C as following.

So whenever we use BOLD it represents integer constant 0 in our code and similarly ITALIC represents 1 and UNDERLINE represents 2.
But there in one more cleaner approach to achieve this same result is by using the enum in C.

What is enum in C?

Enumerator(enum) is one of the special user-defined datatype in C programming language which is used to create and store the integer constants.

Enum in C langauge is used to write clean, easy to read and easy to maintainable code.

What is enum in C

The enum keyword is used to create the enumerated data type in C. Following is an syntax of enum declaration in C:

In above code the textEditor is the name for enumerator datatype and BOLD, ITALIC, UNDERLINE are different enum names separated by a comma.

Examples of enum declaration

We can declare the variable of enumerator type in 2 different ways as follows:

Declaration 1

In the above example we declared the variable feature just after the braces.

Declaration 2

Here we declared the feature variable of type enum inside the main function.

Initialization of Enum values

First value is by default 0

The first enum name in the following declaration is by default assigned to value 0 if it is not initialized and next enum names are assigned by increment of 1.
i.e. BOLD, ITALIC & UNDERLINE will have values 0, 1 & 2 respectively.

Code example

Output

Initializing the values

We can initialize the values to the enum names and then next enum names follow the same increment pattern of 1. For eg.

In above declaration the value of BOLD and ITALIC is 5 and 9 respectively as initialized. The value of UNDERLINE is 10 because every element in enum takes the next integer value of its previous if it is not initialized.

Code example

Output

Defining enum variables by their integer equivalent values

We can directly define the enum variables by directly assigning the equivalent integer values as below code.

Code example

output

In above code if we directly initialize the 5 in a feature variable then the same value gets evaluated. i.e. BOLD.

Initializing the same values

We can initialize the same values to multiple enum names.
For eg. in following declaration enum names brake and stop will have the same value 0.

All enum names must be unique

All enum names must be unique in there scope. For eg. enum bike and car should not contain the same enum name as run.

The Output of above code will generate the error as follows:

Utilizing switch/case statements with enum

Enum in C programming can be great utilized with switch case statements. Enum provides a great way to define the cases so that it becomes easy to modify the code later.
See following code example for implementation.

Code example 4

Output

Using enums for flags

Let's consider the same example as above where we want to design a text editor but now we want the freedom to combine 2 or more features together.

This time we will assign the numbers in power of 2 format with the purpose so that we can combine 2 or more features together usingbit-wise OR operator as follows.

Above numbers if converted to binary then they will look something like following and after performing bit-wise OR (|) operation we can use 2 features combined as explained below.

By doing bit-wise OR operation we got 5 as a result by which we know that both BOLD and UNDERLINE features are used.

Enum vs Macros

The key fundamental difference between enum in C and macros in C is that macros can take any data types even it can take loops, conditionals and function calls with them.
For eg.

But enum in C can only take the integer constants and they provide the clean way to declare multiple values in a single scope of braces as we discussed above.

It is a good idea to use enum over macros if we want to use the multiple well-structured values of type integer.

Conclusion

In this article we learned about what is enum in C language.

  1. We use enums in our code to make better group of constants than macros in terms of readability and functionality.
  2. In C language there are different ways provided to declare the enums and use them.
  3. We saw different ways to initialize the enums and use them with various examples.