12
#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.

5
  • Could you please indicate those compilation errors? Commented Oct 4, 2012 at 4:51
  • Working fine. Commented Oct 4, 2012 at 4:52
  • 2
    @iammilind Try with C. I am talking about C here. I will edit that in main body. Commented Oct 4, 2012 at 4:54
  • Not an exact duplicate, but close: stackoverflow.com/questions/3025050/… Commented Oct 4, 2012 at 5: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. Commented Oct 30, 2014 at 4:44

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

3 Comments

what is IOW ? in C++ first class loading takes place?
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.
@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.
6

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.