Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 314
disquisitiones Avatar asked Oct 15 '16 18:10

disquisitiones


People also ask

What is compile time constant in C?

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.

What is a compile time constant expression?

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)

Is constant expressions are evaluated at compile time?

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.

What does initializer element is not a compile time constant mean?

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.

How to declare a compile time constant in C?

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.

How to define constants in C programming?

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.

What is the difference between compile-time constant and runtime constant?

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.

What is the use of const expression in C++?

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.


1 Answers

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.

like image 89
Jens Gustedt Avatar answered Sep 22 '22 18:09

Jens Gustedt