C Enum

Summary: in this tutorial, you will learn how to use the C enum keyword to define an enumeration type that enhances the code readability.

Introduction to the C enum

Suppose that you need to write a program to manage software bugs. To represent the bug status such as open, assigned, and fixed, you may use integer constants. For example:

#include <stdio.h> int main() { // define the status of a bug int bug_status; // open bug_status = 0; // assigned bug_status = 1; // fixed bug_status = 2; return 0; }
Code language: C++ (cpp)

This solution definitely works. However, the code is not so obvious.

To check if a bug is new, you need to compare the bug_status variable with 0. By looking at the number 0, you will not immediately understand its meaning.

To solve this, C provides the enumeration type that allows you to declare an enumeration.

An enumeration is a set of named integer constants. The following program defines an enumeration type called STATUS and a variable bug_status with the type STATUS:

enum STATUS { open, assigned, fixed } bug_status;
Code language: C++ (cpp)

After that, the bug_status can accept any value specified in the enumeration. For example:

bug_status = open;
Code language: C++ (cpp)

The following program is the same as the one above but use the enumeration:

#include <stdio.h> int main() { // define the bug status enumeration enum STATUS { open, assigned, fixed } bug_status; // open bug_status = open; // assigned bug_status = assigned; // fixed bug_status = fixed; return 0; }
Code language: C++ (cpp)

The code looks obvious and more readable now.

C enum syntax

enum enumeration_type_name { // enumerator-list }
Code language: C++ (cpp)

Here’s the syntax of defining an enumeration type:

In this syntax:

  • First, specify the name of the enumeration type after the enum keyword.
  • Second, specify a comma-separated list of enumeration members.

For example, the following define an enumeration type with the name RGB:

enum RGB { red, green, blue };
Code language: C++ (cpp)

From now, you can declare a variable with the RGB type like this:

enum RGB color = green;
Code language: C++ (cpp)

Or you can combine the enumeration type declaration with the variable declaration like this:

enum RGB { red, green, blue } color; color = green;
Code language: C++ (cpp)

By default, the first member in the enumeration is 0. The next member has the value of the first member plus 1 and son on.

In the following enum:

enum RGB { red, green, blue };
Code language: C++ (cpp)

The red is 0, green is 1, and blue is 2.

To assign a a member of the enumeration a value, you can use the assignment operator. For example:

#include <stdio.h> int main() { enum DAY { monday = 2, tuesday, wednesday, thursday, friday, saturday, sunday }; enum DAY workday = monday; return 0; }
Code language: C++ (cpp)

In this example, we implicitly assign 2 to monday. Therefore, the tuesday will take 3, wednesday takes 4, and so on.

C enum and integers

Since enumeration members are integers, you can use arithmetic operator on them. For example:

#include <stdio.h> int main() { enum DAY { monday = 2, tuesday, wednesday, thursday, friday, saturday, sunday }; enum DAY workday = monday; // workday is 2 workday = workday + 1; // now workday is 3 printf("%d\n", workday); // 3 return 0; }
Code language: C++ (cpp)

How it works.

  • First, assign monday to workday variable. Since the monday is 2, the workday is also 2.
  • Second, increase workday by one and display the workday value. The program dispay 3 as expected.

If you have an integer and you want to cast it to an enumeration member, you need to explicitly cast the value. For example:

#include <stdio.h> int main() { enum DAY { monday = 2, tuesday, wednesday, thursday, friday, saturday, sunday }; enum DAY workday; int weekend = 7; workday = (enum DAY) weekend; printf("%d\n", workday); // 7 return 0; }
Code language: C++ (cpp)

In this example, the following statement casts an integer to the enum:

workday = (enum DAY) weekend;
Code language: C++ (cpp)

It’s important to note that C doesn’t not require a cast however it is recommended. This means that you can do like this:

workday = weekend; // BAD
Code language: C++ (cpp)

Summary

  • Use the enum keyword to declare an enumeration type.
  • The first member of the enumeration defaults to zero. The next member will take the value of the first member plus one, and so on.
  • Use the assignment operator (=) to explicitly assign the integer values to the enumeration members.
Was this tutorial helpful ?