How can I define an enumerated type (enum) in C?
Categories:
Defining Enumerated Types (Enums) in C
Learn how to define and use enumerated types (enums) in C to create sets of named integer constants, improving code readability and maintainability.
In C programming, an enumerated type (enum) is a user-defined data type that consists of a set of named integer constants. Enums are primarily used to make code more readable and maintainable by replacing 'magic numbers' with descriptive names. Instead of using raw integer values, you can use meaningful identifiers, which makes your code easier to understand and less prone to errors.
Basic Enum Declaration and Initialization
The basic syntax for declaring an enum in C involves using the enum
keyword, followed by an optional tag (the enum's name), and then a list of enumerators (the named constants) enclosed in curly braces. By default, the first enumerator is assigned the value 0, the second 1, and so on. You can also explicitly assign integer values to enumerators.
enum Day {
SUNDAY, // 0
MONDAY, // 1
TUESDAY, // 2
WEDNESDAY, // 3
THURSDAY, // 4
FRIDAY, // 5
SATURDAY // 6
};
enum Month {
JAN = 1, // Explicitly assign 1
FEB, // 2 (automatically increments from previous)
MAR, // 3
APR,
MAY,
JUN,
JUL,
AUG,
SEP,
OCT,
NOV,
DEC
};
enum Status {
SUCCESS = 0,
FAILURE = -1,
PENDING = 1
};
Examples of basic enum declarations with default and explicit value assignments.
Using Enums in Your Code
Once an enum type is defined, you can declare variables of that enum type. These variables can then hold any of the values defined by the enumerators. Enums are particularly useful in switch
statements, function parameters, and return types, where they can make the logic much clearer than using raw integer values.
#include <stdio.h>
enum Day {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
};
void print_day_message(enum Day day) {
switch (day) {
case SUNDAY:
printf("It's Sunday! Time to relax.\n");
break;
case SATURDAY:
printf("It's Saturday! Weekend fun.\n");
break;
default:
printf("It's a weekday. Get to work!\n");
break;
}
}
int main() {
enum Day today = WEDNESDAY;
enum Day weekend_day = SATURDAY;
printf("Today is day number %d.\n", today);
print_day_message(today);
print_day_message(weekend_day);
// You can also assign integer values, but it's generally discouraged
// as it bypasses the enum's purpose of named constants.
enum Day invalid_day = 10; // This will compile, but is semantically incorrect
printf("Invalid day value: %d\n", invalid_day);
return 0;
}
Demonstrates declaring enum variables and using them in a switch statement.
flowchart TD A[Define Enum Type] --> B{Declare Enum Variable} B --> C{Assign Enumerator Value} C --> D{Use in Logic (e.g., switch)} D --> E[Improved Readability & Maintainability]
Workflow for defining and using enumerated types in C.
Anonymous Enums and typedef
You can declare an enum without a tag, creating an 'anonymous enum'. However, to declare variables of this type later, you would typically use typedef
to create an alias for the enum. This is a common practice to simplify the syntax when declaring enum variables, allowing you to omit the enum
keyword.
#include <stdio.h>
typedef enum {
RED,
GREEN,
BLUE
} Color;
int main() {
Color my_color = GREEN;
printf("My favorite color is represented by value: %d\n", my_color);
// Using the enum without typedef (requires 'enum' keyword)
enum {
SMALL,
MEDIUM,
LARGE
} size_option;
size_option = MEDIUM;
printf("Selected size: %d\n", size_option);
return 0;
}
Using typedef
with an anonymous enum for cleaner variable declarations.