Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

assert — C23 macro

The `assert(...)` macro from `<assert.h>` checks a scalar expression: true does nothing, while false prints diagnostic information and calls `abort()`. It returns no value.

Version: C23

Syntax

assert(scalar-expression);

The assert(...) macro from <assert.h> checks a scalar expression: true does nothing, while false prints diagnostic information and calls abort(). It returns no value.

Use it to document and check programmer invariants, not user-input failures. The example really checks the result of square(4) before execution continues.

If NDEBUG is defined before the header is included, the check disappears and its argument is not evaluated. Therefore the expression must not contain required side effects; runtime errors need explicit handling that remains in production builds.

Examples

#include <assert.h>
#include <stdio.h>

static int square(int value) { return value * value; }

int main(void) {
    assert(square(4) == 16);
    puts("assertion passed");
    return 0;
}

Result

assertion passed

Use it to document and check programmer invariants, not user-input failures. The example really checks the result of `square(4)` before execution continues.

Sources