Lesson 35 / 36

Undefined Behavior & const

What C lets you get away with — until it doesn't.

No safety net

C trusts the programmer completely: it does not check array bounds, initialise variables, or verify pointers. Violating these rules is undefined behavior — the program might crash, produce garbage, or appear to "work" until it doesn't.

Two classics: overflow and garbage

Both compile cleanly and may even "seem" to run — that's what makes undefined behavior dangerous.

int arr[5];
arr[5] = 99;       // buffer overflow: out of bounds

int x;
printf("%d\n", x); // uninitialised: prints garbage

const is a promise

Marking a parameter const (e.g. void print(const char *s)) tells both the compiler and other readers that the function won't modify it — the compiler then rejects any accidental write.