Why in C a const object is not a compile-time constant expression? [duplicate]
Asked Answered
S

1

8

In C the const qualifier makes an object read-only but not a constant expression. For example, it is not possible to use a const int variable to dimension an array:

const int n = 10;
int arr [n];         /* Compile-time error */

Which is the technical reason for this? Is it not possible for the compiler at compile-time to know that the object has actually a constant value?


I don't think that my question is an exact duplicate of Can a const variable be used to declare the size of an array in C? because I'm not asking if that's possible (it is clearly stated in my question that it is not) but the technical reason why it's not possible.


After the comment of Olaf below, this answer and some musings I would try to summarize and answer my question in this way:

In C a const object is not a compile-time constant because it can violate both requirements:

First, it is possible to initialize the const object at runtime as in:

int i;
scanf ("%d", & i);
const int n = i;

so here we violate the requirement of "known at compile-time".

Secondly, as Olaf pointed out, the const qualifier means that the program itself will not modify the value of the object after the declaration-initialization. But the value of the object in memory could still be modified by some other entity outside the program itself, so here we are not guaranteeing the requirement of actual constness.

Please criticize if this answer is incorrect or incomplete.

Scholem answered 15/10, 2016 at 18:34 Comment(5)
What compiler are you using? I just tried this in GCC 4.8.2 and it compilesUrgency
#18849037Kisser
@UnholySheep: It will work in a function scope because it is defining a VLA (variable length array). You would not be able to initialize that array. It will not work at global scope in standard C — VLAs cannot be defined at global (file) scope. Maybe GCC allows it as an extension, but be aware that it is an extension.Demented
"In C the const qualifier makes an object read-only " - no, it does not! It is just the programmer guaranteeing to the compiler he will not change that object in its declared scope.Florist
@Olaf: thanks for this correction, I think that you hit a fundamental point hereScholem
M
3

One technical reason for this is probably that such a declaration is even valid when the initializer is not a constant expression, so this is a semantic property that is deduced from looking at the initializer.

Then, with the current rules there is no way to have such a thing in a header file to be declared and defined in file scope. Any object can only have one definition, and several object files could not be linked together.

There are ideas to improve that situation for the next version of C.

Mussolini answered 15/10, 2016 at 18:48 Comment(3)
"There are ideas to improve that situation for the next version of C." - you have a reference to support this?Florist
@Olaf: Document N2067 "The register overhaul" from the PrePittsburgh2016 mailing available from WG14 web site — authored by one 'Jens Gustedt'.Demented
@JonathanLeffler Thanks. Btw. I read that name already in the defect reports, not that much of a surprise ;-)Florist

© 2022 - 2024 — McMap. All rights reserved.