What is an Enum?

Young man in office
Richard Drury/Iconica/Getty Images

Enums are found in many Programming Languages

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 in 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.


 #define red 1
 #define orange 2
 const int red=1;

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.:

 enum rainbowcolors {

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.

 enum rainbowcolors {
 } ; 

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

enum rainbowcolors trafficlights= red;

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

rainbowcolors trafficlights = green;

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

rainbowcolors paint = rainbowcolors.red;

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.

 enum trafficlights { alloff = 0,
 yellow = 2,
 red = 4, 
 allon = 7} ;

With this function

 void SetTrafficLights( trafficlights bulb1,trafficlights bulb 2, int timeon) {
 // Simplest way to Or them!
 	int c = (int)a | (int)b; 

Using a Class Instead of Enums

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

SetTrafficlights( red , yellow, 5) ; // 5 seconds of red and yellow

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.


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