Introduction
In the previous chapters, we explored various aspects of C programming, including structures and unions. In this chapter, we will focus on enumerations, commonly known as enums. Enums are a user-defined data type in C that allows you to define a set of named integer constants. They are useful for representing a collection of related values in a readable and maintainable way.
What is an Enum?
An enum (short for enumeration) is a user-defined data type in C that consists of a set of named integer constants called enumerators. Enums make the code more readable by allowing you to use meaningful names instead of numeric values.
Syntax
The basic syntax for defining an enum in C is as follows:
enum enum_name {
enumerator1,
enumerator2,
...
enumeratorN
};
enum
: Keyword used to define an enumeration.enum_name
: Name of the enum.enumerator1
,enumerator2
, …,enumeratorN
: Names of the enumerators.
Example: Defining and Using an Enum
Let’s look at a simple example to understand how to define and use an enum.
Example: Defining and Using a Days of the Week Enum
#include <stdio.h>
// Defining the enum
enum Day {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
};
int main() {
// Declaring a variable of enum type
enum Day today;
// Assigning a value to the enum variable
today = WEDNESDAY;
// Using the enum variable
if (today == WEDNESDAY) {
printf("Today is Wednesday.\n");
}
return 0; // Returning 0 to indicate successful execution
}
Output:
Today is Wednesday.
In this example, we defined an enum named Day
with enumerators representing the days of the week. We then declared a variable of type Day
, assigned it the value WEDNESDAY
, and used it in a conditional statement.
Enumerators
By default, the values of the enumerators start from 0 and increase by 1 for each subsequent enumerator. You can also explicitly assign values to the enumerators.
Example: Explicitly Assigning Values to Enumerators
#include <stdio.h>
// Defining the enum with explicit values
enum Day {
SUNDAY = 1,
MONDAY,
TUESDAY,
WEDNESDAY = 10,
THURSDAY,
FRIDAY,
SATURDAY
};
int main() {
// Declaring a variable of enum type
enum Day today;
// Assigning a value to the enum variable
today = THURSDAY;
// Printing the value of the enum variable
printf("The numeric value of THURSDAY is %d.\n", today);
return 0; // Returning 0 to indicate successful execution
}
Output:
The numeric value of THURSDAY is 11.
In this example, we explicitly assigned values to some of the enumerators. The value of THURSDAY
is automatically set to 11, which is one more than the value of WEDNESDAY
.
Using Enums with Switch Statements
Enums can be effectively used with switch statements to handle multiple cases based on the enum values.
Example: Using Enums with Switch Statements
#include <stdio.h>
// Defining the enum
enum Day {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
};
int main() {
// Declaring a variable of enum type
enum Day today;
// Assigning a value to the enum variable
today = FRIDAY;
// Using the enum variable in a switch statement
switch (today) {
case SUNDAY:
printf("Today is Sunday.\n");
break;
case MONDAY:
printf("Today is Monday.\n");
break;
case TUESDAY:
printf("Today is Tuesday.\n");
break;
case WEDNESDAY:
printf("Today is Wednesday.\n");
break;
case THURSDAY:
printf("Today is Thursday.\n");
break;
case FRIDAY:
printf("Today is Friday.\n");
break;
case SATURDAY:
printf("Today is Saturday.\n");
break;
default:
printf("Invalid day.\n");
}
return 0; // Returning 0 to indicate successful execution
}
Output:
Today is Friday.
In this example, we used the enum Day
in a switch statement to print the name of the day based on the value of the today
variable.
Typedef and Enums
You can use typedef
with enums to create a new type name for the enum, making the code more readable.
Example: Using Typedef with Enums
#include <stdio.h>
// Defining the enum with typedef
typedef enum {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
} Day;
int main() {
// Declaring a variable of the new type
Day today;
// Assigning a value to the enum variable
today = MONDAY;
// Using the enum variable
printf("The numeric value of MONDAY is %d.\n", today);
return 0; // Returning 0 to indicate successful execution
}
Output:
The numeric value of MONDAY is 1.
In this example, we used typedef
to define a new type name Day
for the enum, simplifying the declaration of variables of this type.
Enum Scope and Naming
Enums in C have global scope if defined outside of any function. To avoid naming conflicts, it’s a good practice to use a prefix or a naming convention for enumerators.
Example: Enum Scope and Naming Convention
#include <stdio.h>
// Defining the enum with a naming convention
typedef enum {
DAY_SUNDAY,
DAY_MONDAY,
DAY_TUESDAY,
DAY_WEDNESDAY,
DAY_THURSDAY,
DAY_FRIDAY,
DAY_SATURDAY
} Day;
int main() {
// Declaring a variable of the new type
Day today;
// Assigning a value to the enum variable
today = DAY_FRIDAY;
// Using the enum variable
printf("Today is Friday with enum value %d.\n", today);
return 0; // Returning 0 to indicate successful execution
}
Output:
Today is Friday with enum value 5.
In this example, we used a naming convention (DAY_
) for the enumerators to avoid potential naming conflicts.
Conclusion
Enums in C provide a way to define a set of named integer constants, making the code more readable and maintainable. By understanding how to define, use, and manipulate enums, you can represent a collection of related values in a meaningful way. Enums are especially useful when used with switch statements and can be combined with typedef
for improved readability. Mastering enums is essential for writing clear and efficient C programs.