Enumerations
suggest changeAn enumeration defines a type and a set of named integer constants:
enum state {
state_idle, /* 0 */
state_running, /* 1 */
state_stopped = 5,
state_failed /* 6 */
};
An enumerator without an explicit value is zero when it is first; otherwise, its value is one greater than the previous enumerator. Multiple enumerators may have the same value.
An enumerated type is itself an integer type, and it is compatible with an implementation-chosen type: char, a signed integer type, or an unsigned integer type. Do not assume which type, or the enumeration's size. In C23, an enumeration can explicitly specify a fixed underlying integer type:
enum status : unsigned char {
status_ok,
status_retry,
status_error
};
The fixed-underlying-type syntax is not available in C17 and earlier.
An enum variable is not restricted to the listed enumerator values. Validate integers from files, networks, command lines, and other untrusted sources before converting them to an enum or using them to index a table.