enum
?An enum
is a type for limiting variables to be one of a set of
named constants
enum Name {
first_choice = <integral constant>, // assignment is optional; default is 0
second_choice, // value is previous value + 1
...
nth_choice,
};
enum Name variable = second_choice;
Enums can replace "magic numbers" with meaningful names
// ... using magic constants
// imagine a network request
int status = get(url, buffer);
if (status == 200) { // What is 200??
// ...
} else if (status == 404) { // 404??
// ...
}
// ...
enum Status {
SUCCESS = 200,
NOT_FOUND = 404
};
// imagine a network request
enum Status status = get(url, buffer);
if (status == SUCCESS) {
// ...
} else if (status == NOT_FOUND) {
// ...
}
Values underlying an enum are automatically created if not assigned
enum Orientation {
NORTH, // 0
EAST, // 1
SOUTH,
WEST,
};
Changing values of these constants is done in a single location
Enum constants are not scoped by their enclosing enum block
enum Orientation { NORTH, EAST, SOUTH, WEST, };
// ...
const uint32_t NORTH = 0; // compiler error: namespace is "polluted"
int
enum Orientation direction = EAST; // intention is clear
int choice = EAST; // much harder to understand intention later on
#include <stdio.h>
enum Color {
RED = 5,
GREEN,
BLUE = 5, // outch
};
int main() {
if (RED == BLUE) { // valid (an no compiler warning about this)
puts("what a mess");
}
return 0;
}
enum
Constants#include <stdio.h>
enum Color { RED, GREEN, BLUE, };
enum Orientation { NORTH, EAST, SOUTH, WEST, };
int main() {
if (RED == NORTH) { // valid (at least, decent compilers warn about this)
printf("what a mess\n");
}
return 0;
}
int
#include <stdio.h>
int main() {
enum Color { RED, GREEN, BLUE };
enum Color my_color = 1; // Valid in C! my_color is GREEN
int color_int = my_color; // Implicit conversion
my_color = color_int + 10; // Also valid for compiler! 💀
printf("my_color: %d\n", my_color); // definitely no color 💩
return 0;
}
It is common to use switch
when working with an
enum
#include <stdio.h>
enum Orientation {
NORTH,
EAST,
SOUTH,
WEST,
};
int main() {
enum Orientation heading = EAST; // user controls movement of character
switch (heading) {
case NORTH:
puts("Hero moves north..."); // in lieu of proper action
break;
case EAST:
puts("Hero moves east...");
break;
case SOUTH:
puts("Hero moves south...");
break;
case WEST:
puts("Hero moves west...");
break;
}
return 0;
}