Go to the first, previous, next, last section, table of contents.


assert

Syntax

#define NDEBUG
#include <assert.h>

assert(expression);
assertval(expression);

Description

These macros are used to assist in debugging. The source code includes references to assert or assertval, passing them expressions that should be true (non-zero). When the expression yields false (zero), a diagnostic message is printed to the standard error stream, and the program aborts.

If you define the macro NDEBUG before including `assert.h', then these assert and assertval expand to nothing to reduce code size after debugging is done.

Return Value

assert returns 1 if its argument is non-zero, else it aborts.

assertval returns the value of its expression argument, if non-zero, else it aborts.

Portability

ANSI (see note 1), POSIX (see note 2)

Notes:

  1. assert is ANSI, assertval is not.
  2. assert is Posix, assertval is not.

Example

/* Like `strdup', but doesn't crash if the argument is NULL.  */
char * safe_strdup(const char *s)
{
  assert(s != 0);
  return strdup(s);
}


Go to the first, previous, next, last section, table of contents.