C Enumeration (enum)

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

What is Enumeration or 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.

Syntax of enum in C

In C, an enum (enumeration) is a user-defined data type that consists of a set of named integer constants. It is often used to define a set of related symbolic names that represent integral values. Here's the syntax of declaring and using an enum in C:

Here's a breakdown of the syntax:

  • enum: This keyword is used to define an enumeration.

  • enumeration_name: This is the name of the enumeration type. It is typically written in uppercase letters to distinguish it from variables and other identifiers. It's optional but is good practice to provide a meaningful name.

  • enumerator1, enumerator2, enumerator3, etc.: These are the individual named constants within the enumeration. Each enumerator is associated with an integral value, which starts at 0 for the first enumerator and increments by 1 for each subsequent enumerator. You can explicitly specify values if needed, like enumerator1 = 5, in which case the following enumerators will continue from there.

After defining an enum, you can use its enumerators in your code. For example:

In this example, selectedColor is a variable of type enum Color, and it's assigned the value GREEN, which is equivalent to 1. You can use these symbolic names for better readability and maintainability in your code.

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.

Facts about initialization of enum

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.