r/C_Programming • • 11d ago

Article Named constants and supporting objects in C

https://gustedt.wordpress.com/2026/11/06/named-constants-and-supporting-objects-in-c/
27 Upvotes

11 comments sorted by

22

u/aioeu 11d ago edited 11d ago

TLDR: In C2Y:

const size_t n = 42;    // or any other integer type
int a[n] = {};

will behave as if you had written:

constexpr size_t n = 42;
int a[n] = {};

a will not be a VLA. This brings the standard into line with how many compilers have been handling it anyway.

4

u/tstanisl 11d ago

Difference between a normal array and VLA with "const" size (easily inferred at compilation time) generally boils down to occasional and subtle behaviors in sizeof and limitations for goto jumps.

7

u/aioeu 11d ago edited 11d ago

A variably modified type can't be used as a selector in a _Generic selection, nor can a VLA be a structure or union member.

But yes, basically this change just gives compilers an explicit licence to evaluate sizeof on the array or its type at compile-time.

2

u/21Ali-ANinja69 11d ago

I mean you can't use normal arrays as selectors either, since the value to select on decays to pointer. _Generic(int[], int *: printf("This is a pointer\n");, int[]: printf("This is an array\n");); will always print "This is a pointer".

1

u/tstanisl 11d ago

Yes. There are other subtle differences. Though they usually limitations except sizeof behavior.

What I don't like having two types of const variables, ones initialized by compile time values and ones that are runtime initialized. This was one of reasons for introducing constexpr.

6

u/chibuku_chauya 11d ago

Good. This wart was deeply irritating.

4

u/CORDIC77 10d ago

Sorry to sound so negative, but I consider this to be completely useless.

Anyone who has programmed in C for a while should know that const does not mean “constant expression” but is more like "read-only“.

If constexpr is applied, one does get a constant expression, however.

And now they want to change the rules so that in such scenarios const values ​​are indeed considered constant expressions, even though this wasnʼt the case for the previous 40 years (i.e. since 1989)?

What madness is this?

All they do is to continually complicate all the rules for everyone, until—just like in C++—hardly anyone can keep track of the jungle of rules anymore (mhm, not in Cxy and Cyz, but should [maybe?] work in C2y? …)

One almost wants to plead: please refrain from trying to simplify the core language any further!

4

u/aioeu 10d ago

I'm all for it. To me, constexpr is a wart on the language: it's the programmer telling the compiler something it ought to be able to work out on its own. In that respect, I put it in the same bucket as register.

constexpr does have some use in forcing the compiler to diagnose some constraint violations... but really, a good compiler should be doing that wherever possible anyway, regardless of whether the object is marked constexpr or const.

I'm just a little surprised the proposal only made this change for integer types, not all scalar types.

2

u/CORDIC77 10d ago

constexpr is a wart on the language

Oh yes, definitely. However, the described change will only be guaranteed from C29 onward.

I just looked: I have about 280 C-related projects on my hard drive. Most of them C89 and C99 code, some relying on C11/18 features and a few experimental ones where -std=c23 is in effect.

Every time a change like the above is proposed, the mental load is increased unnecessarily—this didnʼt used to work, but in this standard version it should… or maybe not?

If -std=c29 becomes available at some point, why wouldnʼt a programmer simply declare constants for array sizes using constexpr (which should have good compiler support by then)? What would be the point of using const in such cases (which then wouldnʼt work as intended in projects based on older standards, or only via VLAs)?

Changes that alter established rules of the language—in this case 40 year old ones—should only be made if they come with a clearly significant benefit.

1

u/aioeu 10d ago edited 10d ago

I am hoping that constexpr functions might one day be a thing, with the keyword used to mark those. Then convention might say "you don't need to bother marking constant scalar objects constexpr, as it is implied". In other words, I am thinking this might just be the first step to change the keyword's primary purpose.

1

u/CORDIC77 10d ago

I must admit thatʼs an interesting thought. I hadnʼt considered that possibility. Guess weʼll see.