| 1 |
'\" t |
| 2 |
.\" Copyright 1993, Thomas Koenig <[email protected]> |
| 3 |
.\" Copyright, the authors of the Linux man-pages project |
| 4 |
.\" |
| 5 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 6 |
.\" |
| 7 |
.TH assert 3 2026-02-08 "Linux man-pages 6.18" |
| 8 |
.SH NAME |
| 9 |
assert \- abort the program if assertion is false |
| 10 |
.SH LIBRARY |
| 11 |
Standard C library |
| 12 |
.RI ( libc ,\~ \-lc ) |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
.B #include <assert.h> |
| 16 |
.P |
| 17 |
.BI "void assert(bool " expression ); |
| 18 |
.fi |
| 19 |
.SH DESCRIPTION |
| 20 |
This macro can help programmers find bugs in their programs, |
| 21 |
or handle exceptional cases |
| 22 |
via a crash that will produce limited debugging output. |
| 23 |
.P |
| 24 |
If |
| 25 |
.I expression |
| 26 |
is false (i.e., compares equal to zero), |
| 27 |
.BR assert () |
| 28 |
prints an error message to standard error |
| 29 |
and terminates the program by calling |
| 30 |
.BR abort (3). |
| 31 |
The error message includes the name of the file and function containing the |
| 32 |
.BR assert () |
| 33 |
call, the source code line number of the call, and the text of the argument; |
| 34 |
something like: |
| 35 |
.P |
| 36 |
.in +4n |
| 37 |
.EX |
| 38 |
prog: some_file.c:16: some_func: Assertion \`val == 0\[aq] failed. |
| 39 |
.EE |
| 40 |
.in |
| 41 |
.P |
| 42 |
If the macro |
| 43 |
.B NDEBUG |
| 44 |
is defined at the moment |
| 45 |
.I <assert.h> |
| 46 |
was last included, the macro |
| 47 |
.BR assert () |
| 48 |
generates no code, and hence does nothing at all. |
| 49 |
It is not recommended to define |
| 50 |
.B NDEBUG |
| 51 |
if using |
| 52 |
.BR assert () |
| 53 |
to detect error conditions since the software |
| 54 |
may behave non-deterministically. |
| 55 |
.SH RETURN VALUE |
| 56 |
No value is returned. |
| 57 |
.SH ATTRIBUTES |
| 58 |
For an explanation of the terms used in this section, see |
| 59 |
.BR attributes (7). |
| 60 |
.TS |
| 61 |
allbox; |
| 62 |
lbx lb lb |
| 63 |
l l l. |
| 64 |
Interface Attribute Value |
| 65 |
T{ |
| 66 |
.na |
| 67 |
.nh |
| 68 |
.BR assert () |
| 69 |
T} Thread safety MT-Safe |
| 70 |
.TE |
| 71 |
.SH STANDARDS |
| 72 |
C11, POSIX.1-2008. |
| 73 |
.SH HISTORY |
| 74 |
C89, C99, POSIX.1-2001. |
| 75 |
.P |
| 76 |
In C89, |
| 77 |
.I expression |
| 78 |
is required to be of type |
| 79 |
.I int |
| 80 |
and undefined behavior results if it is not, |
| 81 |
but in C99 |
| 82 |
it may have any scalar type. |
| 83 |
.\" See Defect Report 107 for more details. |
| 84 |
.SH BUGS |
| 85 |
.BR assert () |
| 86 |
is implemented as a macro; |
| 87 |
if the expression tested has side-effects, |
| 88 |
program behavior will be different depending on whether |
| 89 |
.B NDEBUG |
| 90 |
is defined. |
| 91 |
This may create Heisenbugs which go away when debugging |
| 92 |
is turned on. |
| 93 |
.SH SEE ALSO |
| 94 |
.BR abort (3), |
| 95 |
.BR assert_perror (3), |
| 96 |
.BR exit (3) |