Name | Description | Notes | Source | Availability | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|
_Static_ |
Static assertion | L | + | Keyword | C11 |
Name | Description | Notes | Source | Availability | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|
assert() |
Test assertion | M | (·) | <assert.h> |
C89 | C90 | C95 | C99 | C11 | ||
NDEBUG |
Disable assertion-based debugging | ? | M | User-defined | C89 | C90 | C95 | C99 | C11 | ||
static_ |
Static assertion | M | + | <assert.h> |
C11 |
Assertions are conditions that the programmer believes should always be true. If an assertion is found to be false, it causes a running program to fail, and may prevent the program from even starting (usually during compilation, for example). This helps debugging by ensuring that errors in the programmer's understanding of a program's behaviour are detected before they manifest a more obscure problem during tests.
#include <assert.h>
void assert(scalar expr);
The header <assert.h>
provides a macro assert
for expressing run-time assertions. When an assertion is
executed, its condition is evaluated. If found to be false,
the program is aborted, with a message that may give the
condition that failed, and the line number where it is
asserted. For example:
assert(i == 0);
If NDEBUG
is defined before including <assert.h>
, the
effects of the assert
macro are disabled.
From C11, you can declare static assertions based on constant expressions that should be true even before the program starts running. For example:
_Static_assert (sizeof(char) == 1, "Not C!");
This allows a program to be rejected before it is executed.
the following is also defined:
#include<assert.h>
#define static_assert _Static_assert
So, you can write it like this:
#include<assert.h>
static_assert (sizeof(char) == 1, "Not C!");
- static_assert-declaration
_Static_
assert ( constant-expression , string-literal ) ;