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.

 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%@!

The first statement checks that the arguments to gcd2 are ordered before the recursive calculation begins. section 7.3

If a is greater than b, an enabled assert reports a diagnostic and calls abort. Otherwise, gcd2 checks its termination condition and continues with the remainder calculation. The recursive calls preserve the ordering; the initial caller must ensure it. section 7.3 section 8.7

The C standard library macro assert, declared through assert.h, tests a runtime expression that is expected to hold at a particular point. When NDEBUG is not defined during compilation, the expression is evaluated each time execution reaches the assertion. A false condition produces a diagnostic and terminates the program abnormally via abort. section 8.7

Checking a wrapper’s inputs

The gcd wrapper uses assert(a) and assert(b) to check that both arguments are nonzero. It then orders them before calling gcd2, satisfying gcd2’s precondition. section 7.3

TAKEAWAY: Make function preconditions explicit and use assertions to confirm runtime properties. During debugging, a failed assertion can reveal a false assumption about variable values. section 7.3 section 8.7

Define NDEBUG in production compilations to disable assertions and reduce overhead. This also prevents evaluation of their expressions, so required work must not depend on an assertion being evaluated. Unlike runtime assert, _Static_assert and static_assert test compile-time Boolean expressions. section 8.7

How Modern C covers it

How deep it goes
Explains evaluation, failure through abort, and disabling with NDEBUG, supported by a recursive function and wrapper.
Examples
Uses a recursive greatest-common-divisor function and its wrapper to check argument ordering and nonzero inputs.
What you can do after
Diagnose false runtime assumptions with assert and disable its checks for production using NDEBUG.
How it connects
Connects assertions to function preconditions, recursion, abort, 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.