liveBook · Manning

State

As explained in Modern C

Hidden state is execution state that affects program behavior but is not directly represented as an ordinary program value.

State is the complete condition of a program execution at a given moment. It includes the current point of execution, the data held by objects, intermediate values, hidden state, and—when relevant—outside intervention such as user input. Hidden state affects behavior without appearing as an ordinary program value. section 5.1.1 section 19.0

1   double blub(double const* a, double* b);
2
3   int main(void) {
4     double c = 35;
5     double d = 3.5;
6     printf("blub is %g\n", blub(&c, &d));
7     printf("after blub the sum is %g\n", c + d);
8   }1   double blub(double const* a, double* b) {
2     double myA = *a;
3     *b = 2*myA;
4     return *a;      // May be myA or 2*myA
5   }


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":1}],[{\"line\":7,\"ch\":5},{\"line\":7,\"ch\":6}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":10}],[{\"line\":3,\"ch\":6},{\"line\":3,\"ch\":12}],[{\"line\":4,\"ch\":6},{\"line\":4,\"ch\":12}],[{\"line\":7,\"ch\":9},{\"line\":7,\"ch\":15}],[{\"line\":8,\"ch\":6},{\"line\":8,\"ch\":12}],[{\"line\":0,\"ch\":16},{\"line\":0,\"ch\":28}],[{\"line\":7,\"ch\":21},{\"line\":7,\"ch\":33}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":10}],[{\"line\":3,\"ch\":6},{\"line\":3,\"ch\":12}],[{\"line\":4,\"ch\":6},{\"line\":4,\"ch\":12}],[{\"line\":7,\"ch\":9},{\"line\":7,\"ch\":15}],[{\"line\":8,\"ch\":6},{\"line\":8,\"ch\":12}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":1}],[{\"line\":8,\"ch\":0},{\"line\":8,\"ch\":1}],[{\"line\":9,\"ch\":11},{\"line\":9,\"ch\":12}],[{\"line\":10,\"ch\":39},{\"line\":10,\"ch\":40}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":1}],[{\"line\":3,\"ch\":17},{\"line\":3,\"ch\":18}],[{\"line\":4,\"ch\":17},{\"line\":4,\"ch\":18}],[{\"line\":9,\"ch\":0},{\"line\":9,\"ch\":1}],[{\"line\":2,\"ch\":4},{\"line\":2,\"ch\":7}],[{\"line\":5,\"ch\":8},{\"line\":5,\"ch\":11}],[{\"line\":6,\"ch\":8},{\"line\":6,\"ch\":11}],[{\"line\":2,\"ch\":8},{\"line\":2,\"ch\":12}],[{\"line\":2,\"ch\":13},{\"line\":2,\"ch\":17}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":1}],[{\"line\":10,\"ch\":0},{\"line\":10,\"ch\":1}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":10}],[{\"line\":3,\"ch\":6},{\"line\":3,\"ch\":12}],[{\"line\":4,\"ch\":6},{\"line\":4,\"ch\":12}],[{\"line\":7,\"ch\":9},{\"line\":7,\"ch\":15}],[{\"line\":8,\"ch\":6},{\"line\":8,\"ch\":12}],[{\"line\":3,\"ch\":18},{\"line\":3,\"ch\":19}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":1}],[{\"line\":11,\"ch\":0},{\"line\":11,\"ch\":1}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":10}],[{\"line\":3,\"ch\":6},{\"line\":3,\"ch\":12}],[{\"line\":4,\"ch\":6},{\"line\":4,\"ch\":12}],[{\"line\":7,\"ch\":9},{\"line\":7,\"ch\":15}],[{\"line\":8,\"ch\":6},{\"line\":8,\"ch\":12}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":1}],[{\"line\":5,\"ch\":6},{\"line\":5,\"ch\":12}],[{\"line\":6,\"ch\":6},{\"line\":6,\"ch\":12}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":1}],[{\"line\":5,\"ch\":6},{\"line\":5,\"ch\":12}],[{\"line\":6,\"ch\":6},{\"line\":6,\"ch\":12}],[{\"line\":7,\"ch\":0},{\"line\":7,\"ch\":1}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":1}],[{\"line\":7,\"ch\":5},{\"line\":7,\"ch\":6}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":10}],[{\"line\":3,\"ch\":6},{\"line\":3,\"ch\":12}],[{\"line\":4,\"ch\":6},{\"line\":4,\"ch\":12}],[{\"line\":7,\"ch\":9},{\"line\":7,\"ch\":15}],[{\"line\":8,\"ch\":6},{\"line\":8,\"ch\":12}],[{\"line\":0,\"ch\":16},{\"line\":0,\"ch\":28}],[{\"line\":7,\"ch\":21},{\"line\":7,\"ch\":33}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":10}],[{\"line\":3,\"ch\":6},{\"line\":3,\"ch\":12}],[{\"line\":4,\"ch\":6},{\"line\":4,\"ch\":12}],[{\"line\":7,\"ch\":9},{\"line\":7,\"ch\":15}],[{\"line\":8,\"ch\":6},{\"line\":8,\"ch\":12}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":1}],[{\"line\":8,\"ch\":0},{\"line\":8,\"ch\":1}],[{\"line\":9,\"ch\":11},{\"line\":9,\"ch\":12}],[{\"line\":10,\"ch\":39},{\"line\":10,\"ch\":40}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":10}],[{\"line\":3,\"ch\":6},{\"line\":3,\"ch\":12}],[{\"line\":4,\"ch\":6},{\"line\":4,\"ch\":12}],[{\"line\":7,\"ch\":9},{\"line\":7,\"ch\":15}],[{\"line\":8,\"ch\":6},{\"line\":8,\"ch\":12}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":1}],[{\"line\":3,\"ch\":17},{\"line\":3,\"ch\":18}],[{\"line\":4,\"ch\":17},{\"line\":4,\"ch\":18}],[{\"line\":9,\"ch\":0},{\"line\":9,\"ch\":1}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":1}],[{\"line\":10,\"ch\":0},{\"line\":10,\"ch\":1}],[{\"line\":3,\"ch\":18},{\"line\":3,\"ch\":19}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":1}],[{\"line\":11,\"ch\":0},{\"line\":11,\"ch\":1}]]"}
!@%STYLE%@!

A pointer example makes state changes visible: blub reads one object, modifies another, and may observe an aliased object differently. section 12.3

For an ordinary single-threaded execution, the state is shaped by the executable, the current execution point, object data, and external input. Holding the same data at the same point of execution should determine the same result, abstracting away outside intervention. C therefore encourages reasoning about values and the abstract state machine rather than platform-specific representations. section 5.1.1 section 5.0

A call can change state indirectly

In blub, myA stores the value read through a. The assignment *b = 2*myA changes the object addressed by b. If a and b designate the same object, that assignment also changes what *a denotes; if their base types prevent aliasing, the return value can be determined without treating the objects as the same state location. section 12.3

State changes appear in several forms:

  • Jumps change the point of execution, including function calls and long jumps. section 19.0
  • Value computation changes intermediate values, while side effects store values or perform input and output. section 19.0
  • Hidden state includes lock state, once_flag initialization state, and atomic_flag status. section 19.0
  • Concurrency makes overall state difficult to observe because threads may execute on different cores without a shared reference moment. section 19.0

How Modern C covers it

How deep it goes
Defines state composition and effects, then works through pointer aliasing and concurrency without fully resolving multithreaded observability.
Examples
Uses pointer mutation, aliased arguments, function calls, void expressions, recursive calls, locks, once_flag, and atomic_flag.
What it assumes
Introduces values, data, and the abstract state machine before relying on pointers, function calls, atomics, and threads.
What you can do after
Diagnose/debug state changes by tracking object locations, pointer aliasing, execution points, side effects, and synchronization state.
How it connects
Connects values, data, the abstract state machine, pointers, aliasing, side effects, atomic objects, threads, and synchronization.

Also explained in

Compare how 39 books explain state →

Related concepts

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