Enums
An enum names a set of integer constants and, in the same stroke, a type those constants belong to. The constants are int in portable C. The type is not a closed set: any int will convert to it.
Detailed Explanation
enum Color { RED, GREEN, BLUE }; defines three constants. RED is 0, GREEN is 1, BLUE is 2, unless you assign values: enum Color { RED = 1, GREEN = 2, BLUE = 4 }; is a bit-flag style. A constant without an equals sign is one more than the previous constant. Two names may share a value.
The constants have type int in C. The enum type itself is compatible with some integer type the compiler chooses; it may be int, or a smaller type if all values fit. That choice affects ABI and sizeof. Do not send an enum over the wire without documenting the width and the values.
C does not check that a enum Color c holds one of the named constants. c = 99; is legal. A switch that lists RED, GREEN, and BLUE and has no default will miss that case at run time. Compilers can warn about unlisted enumerators (-Wswitch) and will not warn about an out-of-range assignment unless you turn on extra checks. Treat the names as the API and validate at the boundary if the value came from input.
Enums are the right tool for a closed set of states, for error codes you want to name, and for flags when you define the shifts yourself. They are better than #define RED 0 because the debugger can print the name and because the constants live in a scope you can reason about. They are worse than C++ enum class because they leak into the surrounding namespace: RED is visible as a bare identifier. A common style prefixes every constant: COLOR_RED.
C23 adds some of the missing pieces — fixed underlying types, better constants — but code that targets C17 should assume the classic rules: constants are int, the type is open, and the width is the compiler's choice.
Code Examples
#include <stdio.h>
enum Color {
COLOR_RED = 1,
COLOR_GREEN = 2,
COLOR_BLUE = 4
};
static const char *color_name(enum Color c)
{
switch (c) {
case COLOR_RED: return "red";
case COLOR_GREEN: return "green";
case COLOR_BLUE: return "blue";
}
return "unknown"; /* input may not be a named constant */
}
int main(void)
{
enum Color c = COLOR_GREEN;
printf("%s %d\n", color_name(c), (int)c); /* prints: green 2 */
printf("%s\n", color_name((enum Color)99)); /* prints: unknown */
return 0;
}#include <stdio.h>
enum State { ST_INIT, ST_RUN, ST_DONE };
int main(void)
{
for (enum State s = ST_INIT; s <= ST_DONE; s++) {
printf("%d ", (int)s);
}
printf("\n"); /* prints: 0 1 2 */
return 0;
}Common Gotchas
An enum variable can hold any integer the compiler's chosen type can hold
validation belongs in your `switch` or at the parser, not in the type system.
Enum constants occupy the surrounding namespace
`enum Color { RED };` and `enum Alert { RED };` collide. Prefix them.
Incrementing an enum in a loop assumes contiguous values starting at a known point
gaps and explicit assignments break `s++` as a walk of the named constants.
`sizeof(enum Color)` is not portable
it may be 1, 2, or 4. Use a fixed-width integer in serialized data.