C Programming/C Reference/assert.h/assert

From Wikibooks, open books for an open world
Jump to: navigation, search

Syntax[edit]

#include <assert.h>
void assert(expression);

Return value[edit]

The assert macro returns a void, i.e. no value.

Notes[edit]

The behaviour of the assert macro depends on the value of the NDEBUG macro. See assert.h for more details.

If NDEBUG is defined then the macro assert, regardless of argument, is defined as

#define assert(expression)        ((void) 0)

If NDEBUG is not defined the assert macro acts as a diagnostic test. If the expression argument of the macro is false (see C Programming/Control), then the macro expands to a statement which writes the text of the argument, the present file name, line number and function name to the standard error stream. Finally the assert macro calls the abort() function.

Each time the assert.h header is included the assert macro is redefined depending on the value of NDEBUG.