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.
A compile-time constant is a value that can be (and is) computed at compile-time. A runtime constant is a value that is computed only while the program is running. If you run the same program more than once: A compile-time constant will have the same value each time the application is run.
A compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following: Literals of primitive type and literals of type String (§3.10.1, §3.10.2, §3.10.3, §3.10.4, §3.10.5)
A constant expression gets evaluated at compile time, not run time, and can be used in any place that a constant can be used. The constant expression must evaluate to a constant that is in the range of representable values for that type.
The reason is that your are defining your imageSegment outside of a function in your source code (static variable). In such cases, the initialization cannot include execution of code, like calling a function or allocation a class. Initializer must be a constant whose value is known at compile time.
const in C does not declare a compile-time constant. You can use an enum constant instead if you want to avoid using #define and want a symbolic name that can appear in a debugger. C99 does support VLAs. However, VS2019 does not support C99.
using a const keyword: Using const keyword to define constants is as simple as defining variables, the difference is you will have to precede the definition with a const keyword. Below program shows how to use const to declare constants of different data types: Refer Const Qualifier in C for details. This article is contributed by Chinmoy Lenka.
A compile-time constant is a value that is computed at the compilation-time. Whereas, A runtime constant is a value that is computed only at the time when the program is running. 2. A compile-time constant will have the same value each time when the source code is run. A runtime constant can have a different value each time the source code is run.
The const just means that you can't modify the value of r or now after they've been initialized. Those values clearly cannot be determined until execution time. (C++ has different rules. It does make NUM_FOO a constant expression, and a later version of the language added constexpr for that purpose.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With