Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

static_assert — compile-time assertion

static_assert checks a constant condition during program translation rather than testing it at runtime.

Version: C23; repository compiler flag, production runtime not attested

Syntax

static_assert(integer_constant_expression, "message");
static_assert(integer_constant_expression);
_Static_assert(integer_constant_expression, "message");
_Static_assert(integer_constant_expression);

static_assert requires an integer constant expression. A nonzero value satisfies the condition; zero requires a diagnostic. The declaration performs no runtime test.

In C23, the message is an optional string literal; it should appear in a failed assertion's diagnostic. The semicolon is required. static_assert is a keyword requiring no header. _Static_assert has the same meaning but is a deprecated spelling in C23.

In the example, CAPACITY is an enumeration constant. Both assertions hold; changing its value to 7 violates the second.

Examples

enum { CAPACITY = 8 };

static_assert(CAPACITY > 0, "CAPACITY must be positive");
_Static_assert(CAPACITY % 2 == 0);

int main(void)
{
    return 0;
}

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources