Enumerated Types enum
Last Updated: Jan 2026
An Enumeration (enum) is a custom user-defined data type in C that assigns human-readable names (identifiers) to integer constants.
Instead of using raw, unreadable numbers (like 0, 1, 2) throughout your code to represent states or options, enum lets you assign meaningful names, making your code significantly cleaner, less error-prone, and easier to debug.
You define an enumeration using the enum keyword. By default, the compiler assigns integer values starting from 0 and increments each subsequent member by 1.
Syntax:
enum name {
member1 = value1,
member2 = value2,
...
memberN = valueN
};
Example:
#include <stdio.h>
// Declare the enum type
enum Day {
SUNDAY, // 0
MONDAY, // 1
TUESDAY, // 2
WEDNESDAY, // 3
THURSDAY, // 4
FRIDAY, // 5
SATURDAY // 6
};
int main() {
// Declare an enum variable
enum Day today = WEDNESDAY;
// Enums are printed as integers
printf("Day number: %d\n", today); // Output: 3
if (today == WEDNESDAY) {
printf("Mid-week reached!\n");
}
return 0;
}
Custom Explicit Integer Values
You can manually override default integer assignments. Unassigned members automatically take the value of the previous member plus 1.
#include <stdio.h>
enum Day {
SUNDAY = 1,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
};
enum HttpStatus {
OK = 200,
CREATED = 201,
BAD_REQUEST = 400,
UNAUTHORIZED, // Automatically becomes 401
NOT_FOUND = 404,
SERVER_ERROR = 500
};
int main() {
enum Day today = WEDNESDAY;
enum HttpStatus response = UNAUTHORIZED;
printf("Day number: %d\n", today); // Output: 4
printf("HTTP Error Code: %d\n", response); // Output: 401
return 0;
}
enum with typedef
Writing enum Day every time can feel repetitive. Using typedef creates a clean alias so you can use the name directly like a primitive data type.
typedef enum {
LOW,
MEDIUM,
HIGH,
CRITICAL
} Priority;
int main() {
Priority taskPriority = CRITICAL; // Clean, shorthand declaration
if (taskPriority == CRITICAL) {
printf("Immediate action required!\n");
}
}
