4

I was reading this. The first answer by @Andrei T says that

A "large" object is never a constant expression in C, even if the object is declared as const. Const-qualified objects (of any type) are not constants in C language terminology. They cannot be used in initializers of objects with static storage duration, regardless of their type.

For example, this is NOT a constant

const int N = 5; /* `N` is not a constant in C */

The above N would be a constant in C++, but it is not a constant in C. So, if you try doing

static int j = N; /* ERROR */

you will get the same error: an attempt to initialize a static object with a non-constant

I agree with his answer. I also tried a simple example like following on gcc 4.8.2 & 4.9.2 & it gives compiler errors as I expected:

#include <stdio.h>
int main(void)
{
    const int a=5;
    static int b=a;
    printf("%d",b);
}

But When I tried it on ideone.com it compiles & runs fine and gives expected outcome. See live demo here. Also, on codeblocks 13.12 IDE (gcc 4.7.1) this program runs fine. So, Is it compiler bug or gcc extension? What combination of compiler options ideone uses under the hood? So, how & why it compiles in ideone? What is the reason?

Community
  • 1
  • 1
Destructor
  • 14,123
  • 11
  • 61
  • 126
  • 1
    I suggest you accept Grzegorz Szpetkowski's answer. I've submitted a gcc bug report: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66618 – Keith Thompson Jun 21 '15 at 21:10

2 Answers2

3

This seems to be a gcc speciality. Compiling with -std=c89 or -pedantic reports the error.

Since in all C standards this is a constraint violation, not giving a diagnostic for that case makes gcc without one of the -std=c?? options a non-conforming compiler.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • Mentioning that extension would be helpful. – haccks Jun 21 '15 at 08:31
  • I can't find a set of options that causes gcc *not* to report an error (versions 4.1.2 and 4.8.4). – Keith Thompson Jun 21 '15 at 08:35
  • I searched [GCC C-extensions list](https://gcc.gnu.org/onlinedocs/gcc/C-Extensions.html) and found nothing regarding this. – haccks Jun 21 '15 at 08:37
  • @haccks, it doesn't seem that they document this in the place that you would expect. And actually since this in C this a constraint violation, this isn't even an extension but makes the gcc compiler without specifying one of the standards non-conforming. – Jens Gustedt Jun 21 '15 at 08:41
  • Jens, are you able to compile the code in the question using gcc with *any* set of options? I haven't been able to. – Keith Thompson Jun 21 '15 at 08:49
  • Update: As Grzegorz Szpetkowski's answer says, gcc's `-O` option inhibits diagnosis of this error -- even with `-std=cNN -pedantic`. – Keith Thompson Jun 21 '15 at 09:30
  • 4
    I've submitted a gcc bug report: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66618 – Keith Thompson Jun 21 '15 at 21:11
3

It's because the ideone likely invokes gcc with -O option (optimization level 1). This is the case even for older versions of gcc (mine is 4.4.7):

$ gcc -ansi main.c 
main.c: In function ‘main’:
main.c:6: error: initializer element is not constant
$ gcc -ansi -O main.c
$ echo $?
0

What's interesting here is that with -pedantic it is working properly again and required diagnostic message is present (tested only with 4.4.7, see Keith's comment):

gcc -ansi -pedantic -O main.c 
main.c: In function ‘main’:
main.c:6: error: initializer element is not constant
$ echo $?
1
Community
  • 1
  • 1
Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
  • 4
    I can confirm this behavior on my own system; `-O` (or `-O1`, `-O2`, `-O3`) causes gcc not to diagnose the error. That's a bug in gcc. The use of a non-constant expression to initialize a static object is a constraint violation, requiring a diagnostic. That diagnostic may be a non-fatal warning, but some diagnostic message is mandatory. And I *don't* see the error with `gcc -ansi -pedantic -O main.c` (gcc 4.8.4). – Keith Thompson Jun 21 '15 at 09:22
  • And gcc's behavior is inconsistent. It permits the use of `a` as an initializer for a `static` object, but not in a case label. – Keith Thompson Jun 21 '15 at 09:27