Learn C
#include "structstypes/enums.h"

Enums in C

#concept

Enums name a fixed set of integer constants: enum Day { MON, TUE, WED }; gives MON the value 0, TUE 1, and so on (overridable with = assignments). They replace magic numbers with compiler-checked labels in state machines, error codes, and option sets.

C enums are thin — enumerators leak into the enclosing scope and convert to int freely, so MON from two enums collides. Prefix names (COLOR_RED, ERR_TIMEOUT) to avoid clashes, assign explicit values for protocols, and treat enums as documentation the compiler lightly enforces.

#how it works
1

Name the set

enum Day { MON, TUE, WED }; — MON is 0, TUE 1, WED 2 automatically; explicit = values override any of them.

2

Use in switches

switch (day) with case MON: reads as intent — and -Wswitch warns when an enumerator lacks its case.

3

Prefix everything

Enumerators share the enclosing scope — COLOR_RED vs ERR_OK prefixes prevent cross-enum collisions.

4

They're still ints

Enums convert to int implicitly (printf %d works); the checking is light — names plus switch warnings, not type walls.

#example
#include <stdio.h>
 
enum Traffic { RED, YELLOW, GREEN };
 
int main(void) {
enum Traffic light = GREEN;
if (light == GREEN) {
printf("go\n");
}
printf("%d\n", RED);
return 0;
}
Output
go
0
1light holds the enumerator GREEN — a named state instead of magic 2.
2The comparison reads as English and matches, printing go.
3RED's underlying 0 prints with %d — enumerators are ints underneath.
#use cases
State machines with named, switchable states
Error and status codes readable in debuggers and logs
Option sets replacing scattered #define constants
Explicit values matching protocol and file-format numbers

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C yourself — building beats watching.