Enum Values and Ranges
Control underlying integers.
Assigning Explicit Values
You are not stuck with the default 0, 1, 2 sequence. You can assign any integer to a name using =.
This is useful when the numbers must match an external system, like HTTP status codes.
enum Http { OK = 200, NOT_FOUND = 404, ERROR = 500 };Printing Custom Values
Once assigned, those exact numbers are what get stored and printed.
Here the enum names map directly to standard HTTP codes.
#include <stdio.h>
enum Http { OK = 200, NOT_FOUND = 404, ERROR = 500 };
int main(void) {
printf("%d %d\n", OK, NOT_FOUND);
return 0;
}All lessons in this course
- Defining Enums
- Enum Values and Ranges
- const Variables
- #define Constants