Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Keywords and Statements

The constexpr specifier

Reference for C23 constexpr: declare objects with guaranteed compile-time values, use them in array sizes and static_assert, and know its limits.

C23 adds constexpr as a storage-class specifier for objects. A constexpr object is const and its initializer must be a constant expression, so its value is known at compile time. Unlike a plain const int n = 4;, which C does not treat as a constant expression, a constexpr object can be used in array sizes, case labels, static_assert and as an initializer of other static objects.

Unlike C++, C23 constexpr applies only to objects, not to functions. A function cannot be marked constexpr and cannot be evaluated at compile time.

C
constexpr int size = 8;
int table[size];            /* ordinary array, not a VLA */

Try it

C Constants usable in array sizes and case labels

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Compared with const

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
constexpr type name = constant-expression;
constexpr type name[] = { constant-expressions };
Part Description
constexpr Storage-class specifier. The object is implicitly const.
initializer Required. Must be a constant expression whose value is exactly representable in the object's type.

Returns: nothing; it declares an object.

Errors: a non-constant initializer, an initializer that does not fit the type (a narrowing such as constexpr unsigned char c = 300;), and a pointer-valued initializer that is not a null pointer or an address constant are constraint violations. Taking the address is fine, but you cannot modify the object.

Usage notes

  • Not a function specifier. C has no constexpr functions; for compile-time computation use macros or constant expressions built from operators.
  • Array sizes. Because the value is a constant expression, int a[size] is a normal array, with no VLA overhead and usable at file scope.
  • Types. Allowed object types are scalar types, structures, unions and arrays of those, as long as every initialized part is constant. The type can't be a VLA or a pointer to a non-null address of a runtime object.
  • No mixing with register or auto as storage class in the same declaration (but auto type inference with constexpr is allowed: constexpr auto n = 5;).
  • File scope. A constexpr object at file scope has static storage duration; in a function it has automatic storage unless also static.
  • Replaces the old #define SIZE 4 and enum { SIZE = 4 } tricks for typed constants.
  • Compiler support. GCC 13 and later accept it with -std=c23.