assert
in C
assert
(function macro) aborts the program if the user-specified condition is not true. May be disabled for release builds.
It is defined in header <assert.h>
, much like this:
#ifdef NDEBUG #define assert(condition) ((void)0) #else #define assert(condition) /*implementation defined*/ #endif (until C23) #ifdef NDEBUG #define assert(...) ((void)0) #else #define assert(...) /*implementation defined*/ #endif (since C23)
The definition of the macro assert
depends on another macro, NDEBUG
, which is not defined by the standard library.
If NDEBUG
is defined as a macro name at the point in the source code where <assert.h>
is included, then assert
does nothing.
If NDEBUG
is not defined, then assert
checks if its argument (until C23), or the expression synthesized from __VA_ARGS__
(since C23) (which must have scalar type, otherwise, the behavior is undefined) compares equal to zero. If it does, assert
outputs implementation-specific diagnostic information on the standard error output and calls abort()
. The diagnostic information is required to include the text of expression, as well as the values of the predefined variable __func__
and of (since C99) the predefined macros __FILE__
and __LINE__
.
Notes
There is no standardized interface to add an additional message to assert errors. A portable way to include one is to use a comma operator, or use &&
with a string literal:
assert(("There are five lights", 2 + 2 == 5)); assert(2 + 2 == 5 && "There are five lights");
Even though the change of assert in C23 (N2829) is not a formal defect report, the C committee recommends implementations to backport the change to old modes.
Example
#include <stdio.h> // uncomment to disable assert() // #define NDEBUG #include <assert.h> #include <math.h> #define TEST(...) __VA_ARGS__ int main(void) { double x = -1.0; assert(x >= 0.0); printf("sqrt(x) = %f\n", sqrt(x)); assert(TEST(x >= 0.0)); return 0; }