liveBook · Manning

Assertion

As explained in Modern C

assert is a C standard library macro that evaluates a runtime condition and reports a diagnostic followed by abnormal termination when the condition is false.

Make a precondition visible

Before computing a greatest common divisor, a wrapper checks its inputs with `assert(a);` and `assert(b);`. The recursive helper begins with `assert(a <= b);`. These checks state conditions that are supposed to hold whenever the functions run. section 7.3

 8   size_t gcd2(size_t a, size_t b) {
 9     assert(a <= b);
10     if (!a) return b;
11     size_t rem = b % a;
12     return gcd2(rem, a);
13   }


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":1},{\"line\":0,\"ch\":2}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":11}],[{\"line\":3,\"ch\":7},{\"line\":3,\"ch\":13}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":11}],[{\"line\":3,\"ch\":7},{\"line\":3,\"ch\":13}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":11}],[{\"line\":3,\"ch\":7},{\"line\":3,\"ch\":13}],[{\"line\":1,\"ch\":1},{\"line\":1,\"ch\":2}],[{\"line\":1,\"ch\":7},{\"line\":1,\"ch\":13}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":2}],[{\"line\":2,\"ch\":7},{\"line\":2,\"ch\":9}],[{\"line\":2,\"ch\":15},{\"line\":2,\"ch\":21}],[{\"line\":4,\"ch\":7},{\"line\":4,\"ch\":13}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":2}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":11}],[{\"line\":3,\"ch\":7},{\"line\":3,\"ch\":13}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":2}],[{\"line\":2,\"ch\":15},{\"line\":2,\"ch\":21}],[{\"line\":4,\"ch\":7},{\"line\":4,\"ch\":13}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":2}]]"}
!@%STYLE%@!

In the recursive helper, `assert(a <= b)` checks the argument order before the termination test or remainder calculation. The wrapper arranges that order for the initial call; subsequent calls preserve it. section 7.3

`assert` is a macro from `assert.h` for checking a runtime expression. When `NDEBUG` is not defined during compilation, the expression is evaluated each time execution reaches the assertion. If it is false, a diagnostic is printed and `abort` is called. The diagnostic can help locate a false assumption while debugging; abnormal termination is not a useful way to handle an expected customer-facing condition. section 8.7

TAKEAWAY: Make function preconditions explicit, and use assertions to confirm runtime properties. Define `NDEBUG` in a production compilation to inhibit both expression evaluation and the call to `abort`, reducing overhead. Because evaluation can be switched off, do not put work the program needs inside an asserted expression. For conditions that can be checked at compile time, use `_Static_assert` or `static_assert` instead. section 7.3 section 8.7

How Modern C covers it

How deep it goes
Explains when expressions are evaluated, what happens on failure, and how NDEBUG disables checks; does not detail macro implementation.
Examples
Uses greatest-common-divisor functions that assert nonzero inputs and the recursive helper’s argument order.
What it assumes
Assumes familiarity with C functions, expressions, and compilation; introduces assert.h and NDEBUG in context.
What you can do after
Diagnose/debug with assertions to expose false runtime assumptions, and configure NDEBUG to disable them in production compilations.
How it connects
Links assertions to function preconditions, recursion, abort, NDEBUG, and compile-time assertions.

Also explained in

Compare how 15 books explain assertion →

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.