Skip to chapter content
navan✳THE BUILD LABC17 / GUIDE
0/24 read
CHAPTER 21 / 21 · INPUT, OUTPUT, AND SAFETY

Portability and undefined behavior.

Know which behavior C guarantees and where a program must supply a proof.

BY NAVAN SAMILONG READC17 BASELINETHEORY FIRST
01 / THE MENTAL MODEL

The idea behind the syntax.

C gives programmers close control over representation and memory, but that control comes with explicit boundaries. Undefined behavior means the language imposes no requirements on a program after a particular violation, such as reading outside an array or overflowing a signed integer. It does not mean the program always crashes. A plausible output on one machine can hide the defect until an optimization, input, or target changes.

Implementation-defined behavior is different: the implementation chooses a permitted behavior and documents it. The width of int is an example of a target detail within standard limits. Unspecified behavior means the standard permits several possibilities without requiring one choice, such as the order of many function-argument evaluations. A careful reader identifies which category applies before relying on an observed result.

02 / THE RULES

What C guarantees.

The standard library is portable within its documented contracts, but operating-system calls such as POSIX read, open, and lseek are outside ISO C. They can be excellent tools when a program intentionally targets that system. The boundary should be visible in an adapter or module so the rest of the code need not assume one operating system's file descriptors or error model.

Defensive C design starts with object extents, lifetime, initialization, conversion ranges, and matching format types. Compiler warnings, static analysis, sanitizers, and tests can expose many mistakes, but none replaces a clear proof that every pointer access is within a live object. The example checks an array index before reading it, so its valid behavior follows from the data and the guard rather than from luck.

03 / UNDER THE SURFACE

Why the detail matters.

Undefined behavior is a boundary on the language's guarantees, not a runtime exception category. A compiler may optimize under the assumption that a valid C program never performs it. That can make a defect appear to change unrelated code when optimization is enabled. Bounds, lifetime, signed overflow, and incorrect format arguments are common places to investigate. The right explanation is a source-level proof that the forbidden case cannot occur, not a sample run that happened to print the hoped-for value.

Portability has layers. ISO C defines language and hosted-library behavior; an implementation documents chosen details such as type widths; an operating system defines its own APIs; a project may impose an additional platform contract. Code can intentionally use POSIX or Windows facilities and still be well designed, as long as the dependency is visible. A narrow adapter around platform-specific calls lets the rest of the program use a clear, tested interface instead of carrying assumptions everywhere.

04 / CODE, EXPLAINED

See the rule in context.

portability-and-undefined-behavior.c
#include <stdio.h>
#include <stddef.h>

int main(void) {
    int values[] = {10, 20, 30};
    size_t count = sizeof values / sizeof values[0];
    size_t chosen = 2;
    if (chosen >= count) return 1;
    printf("Chosen: %d\n", values[chosen]);
    return 0;
}
FOLLOW THE EXECUTION

values has three elements, so count is 3. chosen is 2, and the guard allows it because 2 is below 3. Reading values[2] yields 30, so the program prints Chosen: 30. If chosen were 3, the function would return before the invalid access. The guard must remain connected to the same array and index used in the later expression.

05 / THE BOUNDARY

Where the rule stops.

A compiler warning flag or sanitizer run can find problems but cannot make an otherwise invalid pointer safe. Correctness requires the source-level bounds and lifetime argument to hold for every allowed input.

06 / THE CONNECTION

Keep this model.

The reference notes that follow bring together declarations, library facilities, and language-version changes. They help a reader decode unfamiliar C while keeping the central proof obligations visible.

Finished reading?
Built with curiosity. By Navan Sami. ↗C · Types, memory, and the rules behind the code.