Enums pair well with switch because every named value can have a clear branch. This makes state-based code easier to read than scattered integer constants.
Sometimes enum values must match external values such as HTTP-like codes, hardware registers, file formats, or network protocols. In those cases, assign explicit integers.
C enums do not automatically validate values or convert names to strings. Add helper functions when user input, logs, or UI messages need readable names.
An enum gives readable names to integer values, but C does not automatically reject every integer that is outside the declared enumerators. Data read from a file, packet, command line, or database must be checked before the program uses it as a valid state. Centralize conversion so logging, validation, and fallback behavior remain consistent.
Explicit values are important when another system stores or transmits the number. Once a value is public, reordering declarations must not change its meaning. Reserve unknown values where a protocol permits forward compatibility, and decide whether an unrecognized value should be rejected, preserved, or mapped to a safe fallback.
A normal enum usually models one state selected from several alternatives. Independent options need powers-of-two bit flags so values can be combined with bitwise OR and tested with bitwise AND. Mixing sequential enumerators with flags creates combinations that are difficult to read and easy to validate incorrectly.
Do not assume an enum has a particular byte size unless the language version and implementation contract guarantee it. Use an explicitly sized integer in a binary format, then convert that integer to the enum after range checking.
#include <stdio.h>
enum Status {
PENDING,
PAID,
CANCELLED
};
int main() {
enum Status orderStatus = PAID;
switch (orderStatus) {
case PENDING: puts("Waiting for payment"); break;
case PAID: puts("Ship the order"); break;
case CANCELLED: puts("Do not process"); break;
}
}
#include <stdio.h>
enum ErrorCode {
OK = 0,
NOT_FOUND = 404,
SERVER_ERROR = 500
};
int main() {
enum ErrorCode code = NOT_FOUND;
printf("Code: %d\n", code);
}
#include <stdio.h>
enum Status { PENDING, PAID, CANCELLED };
const char* statusName(enum Status status) {
switch (status) {
case PENDING: return "Pending";
case PAID: return "Paid";
case CANCELLED: return "Cancelled";
default: return "Unknown";
}
}
int main() {
printf("%s\n", statusName(PAID));
}
#include <stdio.h>
enum Mode { READ = 1, WRITE = 2, EXECUTE = 3 };
int isValidMode(int value) {
return value == READ || value == WRITE || value == EXECUTE;
}
int main() {
int input = 2;
if (isValidMode(input)) {
enum Mode mode = (enum Mode) input;
printf("Mode: %d\n", mode);
}
}
C enums are represented by integer values and do not automatically reject an integer outside the named constants.
Implicit enum values can change when members are reordered or inserted. If numbers are stored on disk, sent over a network, or shared with another program, that silently changes the contract.
Explore 500+ free tutorials across 20+ languages and frameworks.