liveBook · Manning

NDEBUG

As explained in Modern C, Third Edition

NDEBUG is a C preprocessing macro whose definition conventionally controls whether assertion-related debugging checks are enabled.

NDEBUG is a preprocessing macro from <assert.h> that conventionally selects assertion behavior during compilation. When it is not defined, assert evaluates its Boolean expression whenever execution reaches it; a failed condition prints a diagnostic and calls abort. section 8.8

Debug versus production compilation

Assertions in gcd and gcd2 can expose false assumptions about variable values during debugging. Defining NDEBUG for the whole production compilation inhibits both the assertion expression and the abort call, so erroneous values no longer trigger those checks. section 8.8

 assertion failed in file euclid.h, function gcd2(), line 6

A failed assertion can produce a diagnostic identifying the source file, function, and line before abort terminates execution. section 8.8

Because NDEBUG disables checking rather than merely hiding its output, define it consistently for an entire program or library, typically through the compiler command line rather than a local #define. section 8.8

How Modern C, Third Edition covers it

How deep it goes
Explains expression evaluation, diagnostics, abort behavior, and whole-project configuration, but does not detail preprocessing mechanics.
Examples
Uses gcd and gcd2 assertions plus a diagnostic naming the file, function, and line of failure.
What it assumes
Assumes familiarity with C compilation, preprocessing macros, Boolean expressions, and the assert facility.
What you can do after
Configure whole-project production builds that disable assertion evaluation and termination while retaining checks during debugging.
How it connects
Connects runtime assertions with assert, compile-time checks with static_assert, and termination with abort.

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.