Static assertions
suggest changeA static assertion verifies a constant expression during translation. If the expression is false, the implementation must issue a diagnostic. This makes static assertions useful for assumptions about sizes, limits, and compile-time configuration.
In C23, static_assert is a keyword and its message is optional:
#include <limits.h>
enum { buffer_size = 256 };
static_assert(buffer_size >= 128);
static_assert(sizeof(int) * CHAR_BIT >= 16, "int must hold at least 16 bits");
In C11 through C17, _Static_assert is the language keyword. Including <assert.h> also provides the static_assert macro, and the diagnostic message is required:
#include <assert.h>
#include <limits.h>
static_assert(sizeof(int) * CHAR_BIT >= 16, "int must hold at least 16 bits");
Static assertions are always checked; unlike the runtime assert macro, they are not disabled by defining NDEBUG.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents