
Enums in C
In C, enum
(short for enumeration) is a user-defined data type that lets you assign names to a set of integer constants — making your code clearer and easier to manage. ??
? Basic Syntax
enum #include enum () { enum enum typedef enum { RED, GREEN, BLUE} Color;Color c = GREEN;
Now you don’t need to write enum Color
, just Color
.
? Behind the Scenes
Enums are just integers under the hood:
enum enum Mood myMood = SAD;printf("%d\n", myMood); // Output: 1
? Why Use Enums?
Replaces magic numbers ????
Improves readability & maintainability
Great for states, modes, types, etc.