Everything in C is undefined behavior
AIThis post was created with the assistance of artificial intelligence (AI).

TL;DR

A prominent voice in the programming community asserts that all nontrivial C code contains undefined behavior (UB). This challenges assumptions about safety and correctness in C programming today. The statement underscores widespread issues with language design and compiler assumptions.

A veteran programmer and industry analyst has declared that every nontrivial C program contains some form of undefined behavior, emphasizing the pervasiveness of this issue in 2026. This assertion underscores ongoing concerns about the safety, correctness, and portability of C code, which remains foundational in many systems.

The statement originates from a detailed critique of C and C++, highlighting how modern code invariably involves undefined behavior (UB). The speaker, who has extensive experience with C and C++ over 30 years, points out that common mistakes—such as memory corruption, misaligned access, or incorrect casting—are not just bugs but are often classified as UB by the language standards.

They clarify that UB is not solely about compiler optimizations but fundamentally about the language’s design, which allows many operations to be undefined. For example, dereferencing unaligned pointers or casting pointers to incompatible types can be UB, regardless of whether the code is optimized or not. Such issues are widespread, affecting even seemingly simple code snippets, and are often invisible to programmers until they cause crashes or security vulnerabilities.

The speaker emphasizes that different hardware architectures handle UB differently—some trap, crash, or emulate operations, while others, like x86, may appear to work fine. However, this variability does not negate the fact that the behavior is undefined by the standard and can change unpredictably with compiler updates or hardware changes.

They also highlight that certain operations, such as atomic loads on misaligned data, are UB, and that even creating pointers with incorrect alignment or casting raw byte buffers to typed pointers can trigger UB. This ubiquity makes it impossible to write truly portable, bug-free C code, according to the speaker.

Why It Matters

This statement matters because it challenges long-held assumptions about C’s safety and portability, especially in critical systems. If all nontrivial C code involves UB, then many existing programs may be inherently unreliable or vulnerable to future compiler or hardware changes. It raises questions about the suitability of C for safety-critical applications and suggests a need to reconsider language choices or adopt stricter coding standards.

Furthermore, this critique could accelerate efforts to develop safer programming languages or tools that can better manage or eliminate UB, impacting the future of systems programming.

Amazon

C programming safety tools

As an affiliate, we earn on qualifying purchases.

As an affiliate, we earn on qualifying purchases.

Background

C has been the dominant systems programming language since its creation in the early 1970s. Over decades, developers have been aware of certain UB issues, such as use-after-free or buffer overflows, but the scope of UB has expanded with language standards and compiler optimizations. Recent discussions, including prominent industry voices, have increasingly highlighted that UB is not just about specific bugs but an inherent feature of the language’s design.

The current debate echoes past concerns about C’s safety, but the recent statement emphasizes that UB is unavoidable and omnipresent in nontrivial code, making the language fundamentally less safe than often assumed.

“Every nontrivial C program involves some form of undefined behavior, and this is a systemic issue embedded in the language’s design.”

— Veteran programmer and industry analyst

“UB means the compiler can assume your code is valid, which leads to unpredictable behavior and security risks.”

— Language standards expert

Amazon

memory debugging tools for C

As an affiliate, we earn on qualifying purchases.

As an affiliate, we earn on qualifying purchases.

What Remains Unclear

It remains unclear whether this statement reflects a consensus view or an extreme interpretation. The extent to which all C code is practically affected by UB, and how this impacts existing codebases, is still being discussed. Additionally, the precise implications for safety-critical systems are not yet fully explored.

Amazon

C language static analyzers

As an affiliate, we earn on qualifying purchases.

As an affiliate, we earn on qualifying purchases.

What’s Next

Expect increased scrutiny of C code in industry, with potential shifts toward safer languages or stricter coding standards. Tooling and compiler warnings may evolve to better detect or mitigate UB. Ongoing discussions will likely explore how to address or manage the pervasive presence of UB in C programming.

Amazon

hardware alignment debugging tools

As an affiliate, we earn on qualifying purchases.

As an affiliate, we earn on qualifying purchases.

Key Questions

Does this mean all C code is unsafe?

Not necessarily unsafe in practice, but according to the statement, all nontrivial C code contains some form of undefined behavior, which can lead to unpredictable results or vulnerabilities.

Can we still write correct C programs?

Yes, but it requires careful coding, understanding of UB, and adherence to strict standards. Complete elimination of UB in complex code is effectively impossible.

What are the implications for safety-critical systems?

This raises concerns about relying on C for safety-critical applications, as UB can cause unpredictable failures or security issues. Alternatives or additional safety measures may be necessary.

Will compilers change to prevent UB?

While some tools warn about UB, the language standard itself allows it. Future compiler developments may attempt to mitigate UB, but the fundamental issue stems from language design.

Source: Hacker News

You May Also Like

Spectre Programming Language

Spectre is a new low-level programming language focusing on safety, correctness, and control via type invariants and contract-based checks, now documented for developers.

My I3-Emacs Integration

Developers have created a method to pass keybindings between i3 window manager and Emacs, enabling seamless control and improved workflow for users.

How Pair Programming Cuts Onboarding Time in Half

The secret to halving onboarding time lies in pair programming’s real-time guidance and collaborative learning, transforming new hires into productive team members faster than ever.

Python in 2025: New Features and Improvements

Discover how Python in 2025 is evolving with new features and improvements that could transform your coding experience—continue reading to stay ahead.