Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

constexpr — an object with a translation-time value

In C23, constexpr defines an immutable object whose name can participate in constant expressions.

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

Syntax

constexpr type identifier = constant_expression;

constexpr is a C23 storage-class specifier: it defines an object, not a function. It implicitly adds const; const alone does not provide a constant expression.

An initializer is required, must satisfy constant-expression rules, and must preserve the exact value in the target type. An ordinary function call is insufficient.

Integer count can supply a non-VLA array bound and a static_assert argument. At file scope, constexpr gives internal linkage; at block scope without static, the object has automatic storage duration.

The example requires C23 support. A repository compiler flag does not attest production-runtime behavior.

Examples

#include <stdio.h>

int main(void)
{
    constexpr int count = 4;
    static_assert(count == 4);
    int values[count] = {10, 20, 30, 40};
    printf("%d %d\n", count, values[count - 1]);
    return 0;
}

Result

4 40

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