assert Macro

If the argument is false, an assertion failure message is printed to stderr and the program is aborted.

Include

<assert.h>

Prototype

void assert(scalar expression);

Argument

expression The expression to test.

Remarks

The expression evaluates to zero or non-zero. If zero, the assertion fails and a message is printed to stderr. The message includes the source file name (__FILE__), the source line number (__LINE__), the expression being evaluated and the message. The macro then calls the function abort(). If the macro _VERBOSE_DEBUGGING is defined, a message will be printed to stderr each time assert() is called.

Assertion testing may be turned off without removing the code by defining NDEBUG before including <assert.h>. If the macro NDEBUG is defined, assert() is ignored and no code is generated.

Example

#include <assert.h>

int main(void)
{
  int a;

  a = 2 * 2;
  assert(a == 4); /* if true-nothing prints */
  assert(a == 6); /* if false-print message */
  /* and abort */
}

Example Output

sampassert.c:9 a == 6 -- assertion failed
ABRT

with _VERBOSE_DEBUGGING defined:

sampassert.c:8 a == 4 -- OK
sampassert.c:9 a == 6 -- assertion failed
ABRT