C Programming • C17 baseline, standards-specific features labeled
Structures, Enumerations, and Unions
Group related values with structures and use enumerations to name a finite set of states.
A structure stores several named members together. An enumeration gives readable names to integer constants. A union overlays its members in the same storage, so only the member most recently written should generally be read; use a separate tag to track which value is active.
Example
#include <stdbool.h>
enum task_state {
TASK_PENDING,
TASK_DONE
};
struct task {
const char *title;
enum task_state state;
bool urgent;
};
struct task first = {"Review input handling", TASK_PENDING, true};
What to expect
first groups the task title, its named state, and an urgency flag.
Key idea: Use structures for records, enums for named states, and tagged unions when a value can have multiple forms.