A new keyword added with the C23 standard is constexpr. It’s a storage class specifier that sets a constant value. Unlike the original C language qualifier, const, storage declared with the constexpr is truly constant and cannot be altered, as was demonstrated in last week’s Lesson.
Though the C23 standard is dated for the year 2023, not every compiler is current with it; implementation is happening slowly. Even then, the -std=c23 switch (or -std=c2x) may be required to activate various C23 features, including recognition of the constexpr keyword.
In a terminal window, use the command clang --version or gcc --version to obtain the compiler’s version. On my main computer, clang is version 14.0.0. I’ve installed later versions for testing, but nothing that fully supports C23 on this system.
On my Linux test computer, clang is version 18.1.3, which doesn’t recognize the constexpr keyword even with the -std=c23 switch applied. But I have installed clang version 19 (19.1.1), which does recognize constexpr but (again) only with the -std=c23 switch used.
Here is sample code:
2025_11_29-Lesson-a.c
#include <stdio.h>
int main()
{
constexpr int size = 16;
printf("The value is %d\n",size);
return 0;
}
As with the const qualifier, you must assign a value immediately when using constexpr to declare storage. In this code, int variable size is declared a constant expression and assigned a value of 16. Here’s the output:
Size is 16
To prove how bulletproof the constexpr is, I tried the same trick from last week’s Lesson to reset its value. Here’s an update to the code presented:
2025_11_29-Lesson-b.c
#include <stdio.h>
int main()
{
constexpr int size = 16;
int *s;
printf("The value is %d\n",size);
s = (int *)&size;
*s = 0;
printf("The value is %d\n",size);
return 0;
}
This code builds with no warnings or errors. It runs when compiled with C23 compatibility as described earlier. Here’s the output:
Size is 16
Size is 16
No errors are generated on output, but the constant size is unaltered. I suppose this result confirms the constant’s consistency on a superficial level; at least the storage seems more protected than shown in last week’s Lesson.
I’ve read other interesting about how constexpr can be used, along with some limitations and proper procedures. I’ll most likely cover these details in the future when the C23 standard is more widely implemented. It’s been two years since I last wrote about C23. Things are moving slowly, but at least they’re moving.
I tried compiling your code with the latest gcc using:
gcc ce.c -std=c23 -o ce
and got this:
gcc: error: unrecognized command-line option ‘-std=c23’; did you mean ‘-std=c2x’?
so I tried c2x and it worked.
I lied. I’ve actually got 13.3.0. The latest is 15.2. Sorry! Maybe -std=c23 will work with later versions.
The given flag, -std=c23 works as of GCC 15.0. With GCC 14.x one still has to use -std=c2x to get (some) support for C23. As of v15.2 GCC defaults to C23 out of the box.
I bet the -std=c2x flag will be supported by all future compilers for compatibility.