TLDR Web Dev · ARTICLES & TUTORIALS

UB OR NOT UB: HOW GCC AND CLANG HANDLE STATICALLY KNOWN UNDEFINED BEHAVIOR

diekmann.uk9 min read

Summary from the TLDR Web Dev newsletter

Undefined behavior (UB) in C code allows compilers to optimize code based on the assumption that no UB exists, potentially removing conditions or altering program flow. However, the handling differs between compilers if the compiler statically detects UB, like division by zero: GCC tends to crash the program, while Clang may ignore the undefined code or continue compiling, potentially leading to unexpected results later. This differing behavior is due to different philosophies - LLVM aims for more lenient handling to match developer expectations, while GCC takes a more conservative approach.