liveBook · Manning

Observable state

As explained in Modern C, Third Edition

Observable state is program state whose value or effects can be detected by code or users outside the component that maintains it.

Observable state is the contents of variables and similar entities, together with output, as they evolve during execution. A C compiler may change internal computations or remove variables when the executable preserves those observable states. This freedom belongs to C’s abstract state machine, which permits different platform implementations. section 5.1

Eliminating an unobservable computation

Given double x = 5.0; double y = 3.0; x = (x * 1.5) - y; printf("x is %g\n", x);, a compiler can calculate x as 4.5 before execution and emit printf("x is 4.5\n");. The intermediate assignments disappear because neither their timing nor their temporary state is observable. section 5.1

Optimization remains valid only when it preserves observable results. Reordering or combining operations can become invalid if an intermediate value is used, output is produced, or an operation such as overflow can terminate the program. Type therefore determines optimization opportunities. section 5.1.4

double x = 5.0;
double y = 3.0;
...
x = (x * 1.5) - y;
printf("x is %g\n", x);

Trace the arithmetic from source expressions to the final observable terminal output. section 5.1

How Modern C, Third Edition covers it

How deep it goes
Explains observable state through constant folding and optimization constraints, stopping short of a complete memory-model treatment.
Examples
Uses constant floating-point arithmetic, terminal output, reordered additions, overflow, and floating-point environment state.
What you can do after
Evaluate whether compiler transformations preserve outputs, relevant variable contents, and termination behavior.
How it connects
Connects abstract state machines, values, types, representations, optimization, pure functions, and program state.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.