.TH assert() "" "" "Macro" "Diagnostics (assert.h)" "(Library/diagnostics)"
.PC "Check assertion at run time"
.B "#include <assert.h>"
\fBvoid assert(\fIoutcome\^\fB)
int \fIoutcome\^\fB;\fR
.PP
.II "run time, check assertion"
.II "assertion, check at run time"
.II "check assertion at run time"
.B assert()
checks the value of
.IR outcome ,
which usually is the product of an expression.
If
.I outcome
is false (zero),
.B assert()
sends a message into the standard-error stream and calls
.BR exit() .
It is useful for verifying that a necessary condition is true.
.PP
The error message includes the text of the assertion that failed, the
name of the source file, and the line within the source file that holds
the expression in question.
These last two elements consist, respectively, of the values of the
preprocessor macros
.B "_\ _FILE_\ _"
and
.BR "_\ _LINE_\ _" .
.PP
.B assert()
calls
.BR exit() ,
which never returns.
.PP
To turn off
.BR assert() ,
define the macro
.B NDEBUG
prior to including the header
.BR assert.h .
This forces
.B assert()
to be redefined as
.DM
	#define assert(ignore)
.DE
.\".SH Example
.\"For an example of
.\".BR assert() ,
.\"see the Lexicon entry for
.\".BR notmem() .
.SH "See Also"
.Xr exit(), exit
.Xr assert.h, assert.h
.Xr "C preprocessor," c_preproc
.br
\*(AS, \(sc7.2.1.1
.br
\*(PX Standard, \(sc8.1
.SH Notes
The ANSI Standard requires that
.B assert()
be implemented as a macro, not a library function.
If a program suppresses the macro definition
in favor of a function call, its behavior is undefined.
.PP
Turning off
.B assert()
with the macro
.B NDEBUG
will affect the behavior of a program if the expression being evaluated
normally generates side effects.
.PP
.B assert()
is useful for debugging, and for testing boundary conditions for which
more graceful error recovery has not yet been implemented.
