Introduction
Enumerations help give names to numbers, making code easier to read and understand.
Enumerations help give names to numbers, making code easier to read and understand.
enum EnumName {
VALUE1,
VALUE2,
VALUE3
};Each name in the enum represents an integer starting from 0 by default.
You can assign specific numbers to names if needed.
enum Day {
SUNDAY,
MONDAY,
TUESDAY
};enum Color { RED = 1, GREEN = 2, BLUE = 4 };
This program uses an enum to represent traffic light colors. It prints a message based on the current light.
#include <stdio.h> enum TrafficLight { RED, YELLOW, GREEN }; int main() { enum TrafficLight light = RED; if (light == RED) { printf("Stop\n"); } else if (light == YELLOW) { printf("Get ready\n"); } else { printf("Go\n"); } return 0; }
Enumerations make your code easier to read and less error-prone.
By default, enum values start at 0 and increase by 1.
You can use enums to improve code clarity instead of using raw numbers.
Enumerations give meaningful names to numbers.
They help avoid mistakes by limiting values to a set list.
Enums make code easier to read and maintain.
enum Day { MON=1, TUE, WED };
int main() {
enum Day today = TUE;
printf("%d", today);
return 0;
}enum Status {
OK = 0,
ERROR = 1,
UNKNOWN = 1
};Pass the test and this pattern moves unknown