What Is an Enum in Programming Languages?

Young man sitting at computer

 Richard Drury/Iconica/Getty Images

Short for enumeration, an enum variable type can be found in C (ANSI, not the original K&R), C++ and C#. The idea is that instead of using an int to represent a set of values, a type with a restricted set of values is used instead.

For example, if we use the colors of the rainbow, which are

  1. Red
  2. Orange
  3. Yellow
  4. Green
  5. Blue
  6. Indigo
  7. Violet

If enums didn't exist, you might use a #define (in C) or const in C++/C# to specify these values. Eg

Too Many Ints to Count!

The problem with this is that there are many more ints than colors. If violet has the value 7, and the program assigns a value of 15 to a variable then it is clearly a bug but might not be detected as 15 is a valid value for an int.

Enums to the Rescue

An enum is a user-defined type consisting of a set of named constants called enumerators. The colors of the rainbow would be mapped like this.:

Now internally, the compiler will use an int to hold these and if no values are supplied, red will be 0, orange is 1 etc.

What Is the Benefit of an Enum?

The point is that rainbowcolors is a type and only other variables of the same type can be assigned to this. C is easier going (ie less strictly typed), but C++ and C# won't allow assignment unless you force it by using a cast.

You aren't stuck with these compiler generated values, you can assign your own integer constant to them as shown here.

Having blue and indigo with the same value isn't a mistake as enumerators might include synonyms such as scarlet and crimson.

Language Differences

In C, the variable declaration must be preceded by the word enum as in

In C++ though, it is not needed as rainbowcolors is a distinct type that doesn't need the enum type prefix.

In C# the values are accessed by the type name as in

What Is the Point of Enums?

Using enums increase the level of abstraction and lets the programmer think about what the values mean rather than worry about how they are stored and accessed. This reduces the occurrence of bugs.

Here is an example. We have a set of traffic lights with three bulbs- red, yellow and green. In the UK, the sequence of traffic lights changes in these four phases.

  1. Red - Traffic Stopped.
  2. Both Red and Yellow - Traffic Still stopped, but lights about to change to green.
  3. Green - Traffic can move.
  4. Yellow - Warning of imminent change to red.

Traffic Light Example

The lights are controlled by writing to the bottom three bits of a control byte. These are laid out as a bit pattern below in binary where RYG represent the three bits. If R is 1, the red light is on etc.

In this case, it's easy to see that the four states above correspond to values 4 = Red on, 6= Red + Yellow both on, 1 = Green on and 2 = Yellow on.

With this function

Using a Class Instead of Enums

In C++ and C# we'd need to create a class and then overload the operator | to allow OR-ing of types trafficlights.

By using enums we prevent problems with other bits being assigned to the bulb control byte. It might be that some of the other bits control self-testing or a "Green Lane" switch. In that case, a bug that allows these bits to be set in normal use could wreak havoc.

To be sure, we'd mask the bits in the SetTrafficlights() function so no matter what value is passed in, only the bottom three bits are changed.

Conclusion

Enums have these benefits:

  • They restrict the values that the enum variable can take.
  • They force you to think about all the possible values that the enum can take.
  • They are a constant rather than a number, increasing readability of the source code
Format
mla apa chicago
Your Citation
Bolton, David. "What Is an Enum in Programming Languages?" ThoughtCo, Apr. 5, 2023, thoughtco.com/what-is-an-enum-958326. Bolton, David. (2023, April 5). What Is an Enum in Programming Languages? Retrieved from https://www.thoughtco.com/what-is-an-enum-958326 Bolton, David. "What Is an Enum in Programming Languages?" ThoughtCo. https://www.thoughtco.com/what-is-an-enum-958326 (accessed March 28, 2024).