D
devdatta_clc
Hi C experts
I've a bunch of questions.
Consider this simplified piece of code.
const int a = 10;
int main () {
static int b = a;
return b;
}
~>gcc test.c
test.c: In function `main':
test.c:4: error: initializer element is not constant
~>
Question1
I understand that const is 'read-only' but I could not convince my
co-worker that its value can change.
6.7.8 Initialization
[...]
[#4] All the expressions in an initializer for an object
that has static storage duration shall be constant
expressions or string literals.
Can someone point me to text that says that 'const' is not 'constant
expression' and can in fact change?
He claims that the compiler may choose to substitute the value at
compile time.
I tried to explain how the value can change (but I had to use pointers
to const int) which he shot down saying the comparison of examples is
not valid (it's not initializer and it involves pointers).
Question2
Moreover, if we turn on optimization, (and assume that the real code
does not have any dead code like above), the compiler does not give any
error. Is the compiler correct after optimisation?
I think it is a bug in compiler but I need text in standard to prove
that.
Thanks in advance for your comments
-Dev
I've a bunch of questions.
Consider this simplified piece of code.
const int a = 10;
int main () {
static int b = a;
return b;
}
~>gcc test.c
test.c: In function `main':
test.c:4: error: initializer element is not constant
~>
Question1
I understand that const is 'read-only' but I could not convince my
co-worker that its value can change.
6.7.8 Initialization
[...]
[#4] All the expressions in an initializer for an object
that has static storage duration shall be constant
expressions or string literals.
Can someone point me to text that says that 'const' is not 'constant
expression' and can in fact change?
He claims that the compiler may choose to substitute the value at
compile time.
I tried to explain how the value can change (but I had to use pointers
to const int) which he shot down saying the comparison of examples is
not valid (it's not initializer and it involves pointers).
Question2
Moreover, if we turn on optimization, (and assume that the real code
does not have any dead code like above), the compiler does not give any
error. Is the compiler correct after optimisation?
I think it is a bug in compiler but I need text in standard to prove
that.
Thanks in advance for your comments
-Dev