An enumeration in C, abbreviated as enum
, is a user-defined data type that allows a programmer to assign names to a set of integral constants. This improves code readability and maintainability by replacing numeric constants with meaningful names.
Syntax of enum
enum enum_name {
constant1,
constant2,
constant3,
...
};
enum_name
: The name of the enumeration.constant1, constant2, ...
: The enumeration constants (identifiers).
How enum
Works
- Each constant in the
enum
is assigned an integer value automatically, starting from0
by default. - The values can also be explicitly specified.
- Once defined, the constants can be used in the program to represent the respective values.
Example of enum
1. Basic Example
#include <stdio.h>
enum Day {
SUNDAY, // 0
MONDAY, // 1
TUESDAY, // 2
WEDNESDAY, // 3
THURSDAY, // 4
FRIDAY, // 5
SATURDAY // 6
};
int main() {
enum Day today;
today = WEDNESDAY;
printf("Day number: %d\n", today); // Output: 3
return 0;
}
2. Enum with Explicit Values
#include <stdio.h>
enum Status {
SUCCESS = 1,
FAILURE = -1,
PENDING = 0
};
int main() {
enum Status taskStatus;
taskStatus = SUCCESS;
if (taskStatus == SUCCESS) {
printf("Task completed successfully!\n");
}
return 0;
}
Properties of enum
- Default Values: The constants start from
0
by default, incrementing by1
for each subsequent constant. - Explicit Assignment: You can assign specific integer values to constants.
- Reusability: The same constant names can’t be reused in other
enum
definitions within the same scope.
Applications of enum
- Improved Readability: Use named constants instead of integers for better code understanding.
- Example:
MONDAY
is more descriptive than1
when referring to days.
- Example:
- State Management: Useful for managing states in a program, such as task status, error codes, etc.
- Code Maintenance: Easy to add, remove, or modify constants without affecting other parts of the program.
Points to Note
- Type Compatibility: The
enum
type is essentially an integer, so enum variables can interact with integer values, but this should be done cautiously. - Scope: The constants in an
enum
are global within the scope of theenum
. - Size: Enum constants are typically of
int
type unless otherwise specified.
Advanced Example
Enum with Multiple Constants Sharing the Same Value
#include <stdio.h>
enum Color {
RED = 1,
GREEN = 2,
BLUE = 3,
DEFAULT = 1 // Same as RED
};
int main() {
printf("RED: %d\n", RED); // Output: 1
printf("DEFAULT: %d\n", DEFAULT); // Output: 1
return 0;
}
Enum with Typedef
#include <stdio.h>
typedef enum {
SMALL = 1,
MEDIUM,
LARGE
} Size;
int main() {
Size shirtSize = MEDIUM;
printf("Shirt size: %d\n", shirtSize); // Output: 2
return 0;
}
Limitations of enum
- No Type Safety: Enum constants are treated as integers, which may lead to unintended operations.
- Fixed Size: Enum constants are of type
int
, which might be wasteful in terms of memory for large enumerations.
Conclusion
enum
is a powerful feature in C for defining a collection of named integer constants. It enhances code clarity and reduces the chance of errors compared to using raw numeric values directly.