7

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.

Community
  • 1
  • 1
disquisitiones
  • 167
  • 2
  • 11
  • 1
    What compiler are you using? I just tried this in GCC 4.8.2 and it compiles – UnholySheep Oct 15 '16 at 18:39
  • 4
    http://stackoverflow.com/questions/18848537/can-a-const-variable-be-used-to-declare-the-size-of-an-array-in-c – nempat Oct 15 '16 at 18:40
  • 1
    @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. – Jonathan Leffler Oct 15 '16 at 19:00
  • 1
    "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. – too honest for this site Oct 15 '16 at 19:17
  • @Olaf: thanks for this correction, I think that you hit a fundamental point here – disquisitiones Oct 15 '16 at 21:05

1 Answers1

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.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177