xref: /freebsd/share/man/man3/assert.3 (revision a259b98fa211ed87bfee58c575de4e2de94ee0fa)
1.\" Copyright (c) 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.Dd May 17, 2026
29.Dt ASSERT 3
30.Os
31.Sh NAME
32.Nm assert ,
33.Nm static_assert
34.Nd expression verification macro
35.Sh SYNOPSIS
36.In assert.h
37.Fn assert expression
38.Fn static_assert expression
39.Fn static_assert expression message
40.Sh DESCRIPTION
41The
42.Fn assert
43macro tests the given scalar
44.Ar expression ,
45and if it is false,
46a diagnostic message is written to
47.Dv stderr
48and the function
49.Xr abort 3
50is called, effectively terminating the calling process.
51.Pp
52If
53.Ar expression
54is true,
55the
56.Fn assert
57macro does nothing.
58.Pp
59In all compilation modes,
60.Fn assert
61is defined as a macro with an ellipsis parameter, consistent with the
62C23 standard.
63This allows expressions containing commas to be passed directly without
64requiring an extra pair of enclosing parentheses.
65Only a single scalar expression is evaluated.
66Supplying multiple arguments is prohibited, and hence are top-level comma
67operators.
68In particular, this guards against accidentally writing
69.Fn assert
70in the style of
71.Fn static_assert ,
72which would otherwise silently evaluate as always true via the comma
73operator.
74.Pp
75The
76.Fn assert
77macro
78may be removed at compile time by defining
79.Dv NDEBUG
80as a macro
81(e.g., by using the
82.Xr cc 1
83option
84.Fl D Ns Dv NDEBUG ) .
85Unlike most other include files,
86.In assert.h
87may be included multiple times.
88Each time whether or not
89.Dv NDEBUG
90is defined determines the behavior of assert from that point forward
91until the end of the unit or another inclusion of
92.In assert.h .
93.Pp
94The
95.Fn assert
96macro should only be used for ensuring the developer's expectations
97hold true.
98It is not appropriate for regular run-time error detection.
99.Pp
100In pre-C23 compilation modes
101.Fn static_assert
102is implemented as a macro and expands to
103.Fn _Static_assert ,
104and, contrarily to
105.Fn assert ,
106makes assertions at compile-time.
107Once the constraint is violated, the compiler produces a diagnostic
108message including the string literal message, if provided.
109The initial form of the
110.Fn _Static_assert
111containing a string literal message was introduced in C11 standard, and
112the other form with no string literal conforms to C23 standard.
113.Pp
114In C23 and later,
115.Fn static_assert
116is a language keyword, and
117.Fn _Static_assert
118is provided as an obsolescent alternative spelling that should not be
119used for new code and development.
120.Sh EXAMPLES
121The assertion:
122.Dl "assert(1 == 0);"
123generates a diagnostic message similar to the following:
124.Dl "Assertion failed: (1 == 0), function main, file main.c, line 100."
125.Pp
126The following assert tries to assert there was no partial read:
127.Dl "assert(read(fd, buf, nbytes) == nbytes);"
128However, there are two problems.
129First, it checks for normal conditions, rather than conditions that
130indicate a bug.
131Second, the code will disappear if
132.Dv NDEBUG
133is defined, changing the semantics of the program.
134.Pp
135The following example asserts that the
136.Va iov_len
137member of the compound literal, reflecting the value of
138.Va len ,
139is non-zero.
140The compound literal contains a comma that is not protected by
141parentheses, which the variadic
142.Fn assert
143macro handles transparently:
144.Dl assert((struct iovec){ buf, len }.iov_len);
145.Pp
146The following asserts that the size of the
147.Vt S
148structure is 16.
149Otherwise, it produces a diagnostic message which points at the
150constraint and includes the provided string literal:
151.Dl "static_assert(sizeof(struct S) == 16, ""size mismatch"");"
152If none is provided, it only points at the constraint.
153.Sh SEE ALSO
154.Xr abort2 2 ,
155.Xr abort 3
156.Sh STANDARDS
157The
158.Fn assert
159macro conforms to
160.St -isoC-2023 .
161.Pp
162The
163.Fn static_assert
164macro conforms to
165.St -isoC-2011 .
166In
167.St -isoC-2023 ,
168it is a language keyword; whether the macro is defined or not
169depends on compilation mode.
170.Sh HISTORY
171The
172.Nm
173macro first appeared in
174.At v7 .
175Starting with
176.Fx 15.2 ,
177it accepts a variadic argument list, allowing expressions
178containing commas, such as compound literals, to be passed
179without requiring extra enclosing parentheses.
180This conforms to
181.St -isoC-2023 ,
182but made available in all compilation modes.
183