Enum Basics
beginner12 min readLesson 54 of 148
An enum is a type whose values are named integer constants.
Declaring and using
enum Color { RED, GREEN, BLUE };
enum Color c = GREEN;
The compiler assigns values starting at 0: RED=0, GREEN=1, BLUE=2. You can pin or offset values:
enum Level { LOW = 1, MEDIUM, HIGH }; // 1, 2, 3
enum Mask { BIT0 = 1, BIT2 = 4 }; // explicit
Why not just ints?
Magic numbers (if (s == 2)) hide meaning; enum names carry it
(if (s == STATE_PAUSED)). The compiler also type-checks the intent, and
switch coverage warnings catch missing cases.
Enums and switch
enum Color invert(enum Color c) {
switch (c) {
case RED: return BLUE;
case BLUE: return RED;
case GREEN: return GREEN;
}
return c; // defensive: reachable only on invalid input
}
Each case names a constant โ no more guessing what 1 meant.