liveBook · Manning

Runtime assertion

As explained in Modern C

A runtime assertion is a check performed during program execution that verifies a condition expected to hold at that point.

A runtime assertion checks a Boolean condition during execution at a point where the condition is expected to hold. In C, assert accepts dynamic expressions; when NDEBUG is defined during compilation, the expression is not evaluated. section 8.7

A failed assertion

A gcd implementation can assert a condition that should hold on every execution. If it fails, execution produces a diagnostic such as “assertion failed in file euclid.h, function gcd2(), line 6” and calls abort. section 8.7

Use runtime assertions to:

  • Expose false assumptions about variable values during debugging. section 8.7
  • Disable assertion evaluation and abort calls in production compilations with NDEBUG. section 8.7

How Modern C covers it

How deep it goes
Explains expression evaluation, failure diagnostics, abort behavior, and production disabling, but gives no implementation internals.
Examples
Uses gcd and gcd2 conditions plus a failed euclid.h diagnostic naming the function and source line.
What it assumes
Assumes C Boolean expressions and distinguishes runtime checks from compile-time assertions.
What you can do after
Apply assert to detect violated runtime assumptions during debugging and use NDEBUG to remove its production overhead.
How it connects
Connects runtime assertions with compile-time assertions, assert.h, NDEBUG, debugging, and abort.

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.