| Main index | Section 3 | 日本語 | Deutsch | Options |
#include <assert.h>
If expression is true, the assert() macro does nothing.
The
assert()
macro
may be removed at compile time by defining
NDEBUG
as a macro
(e.g., by using the
cc(1)
option
The assert() macro should only be used for ensuring the developer's expectations hold true. It is not appropriate for regular run-time error detection.
The static_assert() macro expands to _Static_assert(), and, contrarily to assert(), makes assertions at compile-time. Once the constraint is violated, the compiler produces a diagnostic message including the string literal message, if provided. The initial form of the _Static_assert() containing a string literal message was introduced in C11 standard, and the other form with no string literal is to be implemented by C2x and some compilers may lack its adoption at present.
assert(1 == 0);
generates a diagnostic message similar to the following:
Assertion failed: (1 == 0), function main, file main.c, line 100.
The following assert tries to assert there was no partial read:
assert(read(fd, buf, nbytes) == nbytes);
However, there are two problems. First, it checks for normal conditions, rather than conditions that indicate a bug. Second, the code will disappear if NDEBUG is defined, changing the semantics of the program.
The following asserts that the size of the S structure is 16. Otherwise, it produces a diagnostic message which points at the constraint and includes the provided string literal:
static_assert(sizeof(struct S) == 16, "size mismatch"");"
If none is provided, it only points at the constraint.
The static_assert() macro conforms to ISO/IEC .
| ASSERT (3) | April 20, 2021 |
| Main index | Section 3 | 日本語 | Deutsch | Options |
Please direct any comments about this manual page service to Ben Bullock. Privacy policy.
| “ | UNIX is a four-letter word! | ” |