Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

static_assert — compile-time assertion

static_assert checks a condition during compilation rather than program execution. In C23 it is a keyword, not a function; its message is optional.

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

Syntax

static_assert(constant-expression, string-literal);
static_assert(constant-expression);

static_assert is a declaration ending with a semicolon. It needs no <assert.h>. Its condition must be an integer constant expression with a nonzero value; an ordinary runtime variable is insufficient.

A zero condition requires a compiler diagnostic. The optional message is a string literal. A successful assertion performs no operation; unlike assert, it does not depend on NDEBUG.

The example checks that CAPACITY is positive and even, then prints OK. It requires C23 support; a repository compiler flag does not establish the production environment.

Examples

#include <stdio.h>

enum { CAPACITY = 16 };
static_assert(CAPACITY > 0, "CAPACITY must be positive");
static_assert(CAPACITY % 2 == 0);

int main(void)
{
    puts("OK");
    return 0;
}

Result

OK

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