Back to C Programming
2026-02-066 min read

17 Enumeration Types (C Programming)

Learn 17 Enumeration Types (C Programming) step by step with clear examples and exercises.

Title: 17 Enumeration Types (C Programming)

Why This Matters

Enumeration types, or enums, are a crucial feature of C programming that allow you to create custom data types consisting of named integer constants. They play an essential role in organizing and managing code, especially in large projects, as they improve readability and maintainability. Enumerations can be used in various scenarios such as defining error codes, representing logical states, or creating custom data structures. In interviews, understanding enums demonstrates your proficiency in C programming.

Prerequisites

To follow this lesson, you should have a basic understanding of:

  • C programming basics (variables, constants, operators)
  • Control structures (if-else, switch-case)
  • Functions and function prototypes
  • Pointers (optional but helpful for understanding bitfields)
  • Data Structures (arrays, structs, unions)

Core Concept

Definition

An enumeration is a user-defined data type consisting of a set of named integer constants. The name of the enumerated data type serves as a tag that identifies the group of related values. Enumerations are defined using the enum keyword followed by the enumerated data type name and a list of enumerators enclosed in curly braces {}.

enum days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY };

In this example, we have defined an enumeration called days, which includes seven enumerators representing the days of the week. By default, the first enumerator has a value of 0, and each subsequent enumerator is incremented by 1.

Bitfields

C also allows you to create bitfields within an enumeration. A bitfield is a data type that occupies a specific number of bits in memory instead of bytes or words. This can be useful when working with hardware registers or optimizing memory usage. To define a bitfield, simply specify the desired number of bits after the enumerator name using the : operator.

enum traffic_light { RED = 3, YELLOW = 2, GREEN = 1, OFF = 0 };
enum { ON : 4 }; // This creates a bitfield with 4 bits

In this example, we have defined an enumeration called traffic_light, which represents the states of a traffic light. Additionally, we've created a separate bitfield called ON with 4 bits.

Enumerations and Memory

When you declare an enumerated data type, the compiler assigns memory to it based on its underlying integer type (char, short, int, or long). By default, C uses int as the underlying type for enums. To change the underlying type, use the -E flag followed by the desired type when compiling the program.

enum days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY };
enum days e = SUNDAY; // Assuming int as underlying type
char c = e; // Assigning the value of the enumerator to a char variable

In this example, we have declared an enumeration called days, and assigned one of its enumerators to a char variable. Since C uses int as the default underlying type for enums, the value of the enumerator is implicitly converted to int before being assigned to the char variable.

Enumerations and Arithmetic Operations

Enumerated constants can be used in arithmetic operations like any other integer constants. However, Note that that enumerators are not interchangeable with their underlying type values directly, as they may have different bit representations due to padding or alignment. To perform arithmetic operations on enumerations, always use the enumerated data type name.

enum days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY };
enum days day1 = SUNDAY;
enum days day2 = WEDNESDAY;
enum days day3 = (day1 + 2) % 7; // Performs arithmetic operation on enumerated constants

In this example, we have performed an arithmetic operation on two enumerators to calculate the next day of the week. Since enumerators are not interchangeable with their underlying type values directly, using the modulo operator % ensures that the result is within the valid range (0-6).

Worked Example

Let's create a simple program that demonstrates enumerations and bitfields:

#include <stdio.h>

enum traffic_light { RED = 3, YELLOW = 2, GREEN = 1, OFF = 0 };
enum { ON : 4 }; // Bitfield with 4 bits

void display_traffic_light(enum traffic_light light) {
switch (light) {
case RED:
printf("Red\n");
break;
case YELLOW:
printf("Yellow\n");
break;
case GREEN:
printf("Green\n");
break;
default:
printf("Off\n");
}
}

int main() {
enum traffic_light light = ON; // Assigning the value of the bitfield to an enumerated variable
display_traffic_light(light);
return 0;
}

In this example, we have defined an enumeration called traffic_light, which represents the states of a traffic light. We've also created a separate bitfield called ON with 4 bits. The display_traffic_light() function takes an enumerated variable as its argument and displays the corresponding state of the traffic light.

Common Mistakes

  1. Forgetting to include the curly braces when defining an enumeration.
enum days SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY; // Incorrect
enum { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }; // Correct
  1. Trying to perform arithmetic operations on enumerators as if they were their underlying type values.
int red = RED; // Incorrect
enum days day1 = SUNDAY + 3; // Incorrect

Instead, use the enumerated data type name when performing arithmetic operations:

enum days day2 = (day1 + 3) % 7; // Correct
  1. Assuming that enumerators are interchangeable with their underlying type values directly.
char c = RED; // Incorrect

Instead, use the % operator to ensure that the result is within the valid range (0-6):

char c = RED % 8; // Correct
  1. Not understanding the difference between enumerators and their underlying type values.

Enumerators are not directly interchangeable with their underlying type values due to padding or alignment, so it is essential to use the enumerated data type name when performing arithmetic operations or assigning values to other data types.

Practice Questions

  1. Create an enumeration called shapes with enumerators representing different geometric shapes. Write a function that takes an enumerated variable as its argument and displays the corresponding shape name.
  2. Create an enumeration called errors with enumerators representing different error codes. Write a function that takes an enumerated variable as its argument and displays the corresponding error message.
  3. Modify the traffic_light example to include the OFF state in the switch statement, and add a new bitfield called BLINKING with 2 bits.
  4. Create a program that uses an enumeration to represent different types of fruits, and another enumeration to represent their colors. Write a function that takes two enumerated variables as arguments (one for fruit type and one for color) and displays the corresponding fruit name and color.
  5. Write a program that creates an enumeration called operations with enumerators representing addition, subtraction, multiplication, and division. Create functions for each operation that take two integer arguments and return their respective results. Use these functions to perform calculations on two user-inputted numbers using the chosen operation.

FAQ

  1. What happens if I don't assign values to enumerators explicitly?
  • If you don't assign values to enumerators explicitly, they will be assigned default values starting from 0. However, it is recommended to assign explicit values for better readability and maintainability.
  1. Can I change the underlying type of an enumeration?
  • Yes, you can change the underlying type of an enumeration by using the -E flag followed by the desired type when compiling the program.
  1. What is the difference between an enumerated data type and a typedef'd data type?
  • An enumerated data type is a user-defined data type consisting of named integer constants, while a typedef'd data type allows you to give a new name to an existing data type. Enumerations provide additional benefits such as improved readability and maintainability, but they are not the same as typedef'd data types.
  1. How can I create an enumeration with custom values for each enumerator?
  • To create an enumeration with custom values for each enumerator, simply assign the desired value to each enumerator when defining the enumeration. For example:
enum days { SUNDAY = 0, MONDAY = 1, TUESDAY = 2, WEDNESDAY = 3, THURSDAY = 4, FRIDAY = 5, SATURDAY = 6 };

In this example, we have explicitly assigned custom values to each enumerator of the days enumeration.