Structs & Data Structures · beginner · ~8 min
Name small integer constants.
enum introduces a set of named integer constants. By default the first is 0 and each next is one more, but you can pin values: enum colour { red=1, green=2, blue=4 };.
Use enums for state machines, message kinds, error codes — anything that should be a small set of named ints. Combine with switch for exhaustiveness warnings.
typedef enum { STATE_IDLE, STATE_RUNNING, STATE_DONE } state_t;
state_t s = STATE_IDLE;
int). Don't assume a particular size when packing into a struct.