Back to results for “Undefined Behavior”
C++ › Undefined Behavior

What is the difference between undefined, unspecified, and implementation-defined behavior?

Beginner

Answer

Undefined behavior has no constraints—anything may happen (must be avoided). Unspecified behavior has several allowed outcomes and the implementation need not document which (e.g., order of evaluation of function arguments). Implementation-defined behavior is unspecified but the implementation must document its choice (e.g., sizeof(int)). Only UB is dangerous; the others are just non-portable in defined ways.
Real-world example sizeof(long) is implementation-defined, argument evaluation order is unspecified, and a[5] on a size-3 array is UB.

Common follow-ups: Which must the implementation document? | Which one is dangerous?