9
#include <stdio.h>
int foo(){
    return 1;
}
int main(void) {
    static int q = foo(); 
    return 0;
}

Here is a link for the same. This is a C code and not C++. It compiles and run fine in C++ but not C.

This code was getting compilation error. Can someone please explain why is it getting error? Can static members only be initialized by constant values ? In C++ we need to DEFINE static members after declaring them , why is it not required in C ? I couldn't find any thread with similar query or a good answer.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
h4ck3d
  • 6,134
  • 15
  • 51
  • 74
  • Could you please indicate those compilation errors? – Mark Garcia Oct 04 '12 at 04:51
  • [Working fine](http://ideone.com/FPkQ6). – iammilind Oct 04 '12 at 04:52
  • 2
    @iammilind Try with C. I am talking about C here. I will edit that in main body. – h4ck3d Oct 04 '12 at 04:54
  • Not an exact duplicate, but close: http://stackoverflow.com/questions/3025050/error-initializer-element-is-not-constant-when-trying-to-initialize-variable-w – Matt Oct 04 '12 at 05:01
  • @h4ck3d As below mentioned, Global and static variables can only be initialized with constant expressions known at compile time. so you can do, `int (*q) (void);q=foo;`. this should work in C. – overexchange Oct 30 '14 at 04:44

3 Answers3

7

Global and static variables can only be initialized with constant expressions known at compile time. Calling your foo() function does not constitute using a constant expression. Further, the order in which global and static variables are initialized is not specified. Generally, calling foo() would mean that there must be a certain order, because the function can reasonably expect some other variables to be already initialized.

IOW, in C, neither of your code is executed before main().

In C++ there are ways around it, but not in C.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
  • what is IOW ? in C++ first class loading takes place? – h4ck3d Oct 04 '12 at 05:25
  • 1
    I did not understand the thing about C++ classes, but in C++ global/static objects are constructed before `main()`, which gives you a chance to call a function at initialization time. There are some rules about the order of initialization in C++, or this wouldn't work in C++ either. – Alexey Frunze Oct 04 '12 at 05:27
  • @AlexeyFrunze Yes, it's just like in Java too. Like, one can print to `stdout` even before `main` is entered. By putting `System.out.println` call inside static initialization block. – John Strood Sep 25 '16 at 11:45
5

All the static variables are compile time and the function is giving the output at run time so you are initializing a compile time variable with a run time variable which is not possible so it is giving error.

Another example may be as follows

int main()
{
int p=9;
static int x=p;
}

the above code is also gives you compile time error,The cause is same as above.

pradipta
  • 1,718
  • 2
  • 13
  • 24
1

If you are doing this in C rather than C++ you can only assign static variables values that are available during compilation. So the use of foo() is not permitted due to its value not being determined until runtime.