Enum (Enumeration) in C

Today we will discuss enumeration in C Language Tutorial…Let’s start.

Enumeration in C Language Tutorial

Introduction

Enumerated Types are a special way of creating your own Type in C. The type is a “list of key words”. Enumerated types are used to make a program clearer to the reader/maintainer of the program. For example, say we want to write a program that checks for keyboard presses to find if the down arrow or up arrow has been pressed. We could say: if ( press_value == 32 ).   32 is the computers representation of the down arrow. Or, we could create our own enumerated type with the key words: own_arrow and up_arrow. Then we could say: if ( press_value ==down_arrow ). This second version is much more readable and understandable to the programmer.

Enumerated Types 

Enumerated Types allow us to create our own symbolic names for a list of related ideas. The key word for an enumerated type is enum. For example, we could create an enumerated type for true and false (note: this is done for you by C and is type bool).

We could also create an enumerated type to represent various security levels so that our program could run a door card reader:

These enumerated types can be used like any other type in a program. The type name “Boolean” or “Security_Levels” can be used to define variables or the return type of functions. The actual enumerations can be used directly in the code. For example:

In Essence, Enumerated types provide a symbolic name to represent one state out of a list of states.


Enumerated Types are Not Strings 

It bears repeating: Even though enumerated type values look like strings, they are new key words that we define for our program. Once defined the computer can process them directly. There is no need to use strcmp with enumerated types.

// CORRECT:

// INCORRECT:

// ALSO INCORRECT:

Enums are Not (Really) Integers

It turns out that enumerated types are treated like integers by the compiler. Underneath they have numbers 0,1,2,… etc. You should never rely on this fact, but it does come in handy for certain applications.

For example: our security_levels enum has the following values:


Printing the “string” representation of the type

Note: One of the short comings of Enumerated Types is that they don’t print nicely. To print the “String” associated with the defined enumerated value, you must use the following cumbersome code:

An Enum String Printing Trick

The following “trick” to print enums (in a more compact notation) is made possible because of the following two facts:
1. C arrays are based on index 0.
2. enums have a “builtin” integer value starting at 0.
The trick is to create an array of strings and use the enums to index the array. Here is an example of how it is done:

Thank you for reading 🙂  If you have any doubt just comment below 🙂 Click Here for know about the Storage class in c.

Download our new Android app. You can learn all Embedded Tutorials from your Android Phone easily.

Click Here to Download App!