Undefined behavior is a construct for which the C standard imposes no requirements at all, the program has no defined meaning once it executes UB, and the compiler is free to do literally anything while remaining conforming. The key distinction from an ordinary wrong-value bug is that UB licenses the optimizer: the compiler assumes UB never happens and transforms code based on that assumption, so the effects can be non-local and counterintuitive, code deleted, branches removed, loops rewritten. A wrong-value bug produces a predictable (if incorrect) result you can trace; UB can make x + 1 > x compile to an unconditional return 1, or make a bounds check vanish because the compiler proved the indexing would be UB otherwise. It's a contract violation, not a computation that came out wrong, which is why you can't reason about a program that contains it.
C Programming · Interview question
What is undefined behavior, and how is it different from a bug that produces a wrong value?
A strong answer
What a weak answer sounds like
You know the answer. Do you know what gets you dinged?
Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.
From the lesson
Undefined Behavior & Pitfalls
Why code that works at -O0 breaks at -O2: what undefined behavior is, the catalog of UB every firmware engineer must recognize, and how sanitizers catch it.