Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

initializing static variable with a function call gives compilation error?

Tags:

c

static

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

like image 717
h4ck3d Avatar asked Oct 04 '12 04:10

h4ck3d


People also ask

Are static variables initialized at compile time?

Static variables are initialized only once , at the start of the execution. These variables will be initialized first, before the initialization of any instance variables. A single copy to be shared by all instances of the class. A static variable can be accessed directly by the class name and doesn't need any object.

What happens when a function is declared static?

A static function in C is a function that has a scope that is limited to its object file. This means that the static function is only visible in its object file. A function can be declared as static function by placing the static keyword before the function name.

How do you initialize a static variable?

For the static variables, we have to initialize them after defining the class. To initialize we have to use the class name then scope resolution operator (::), then the variable name. Now we can assign some value. The following code will illustrate the of static member initializing technique.

What happens when a variable is declared static?

When a variable is declared as static, then a single copy of the variable is created and shared among all objects at the class level. Static variables are, essentially, global variables. All instances of the class share the same static variable.


3 Answers

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.

like image 196
Alexey Frunze Avatar answered Oct 10 '22 07:10

Alexey Frunze


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.

like image 23
pradipta Avatar answered Oct 10 '22 06:10

pradipta


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.

like image 1
Patrick Wilkin Avatar answered Oct 10 '22 06:10

Patrick Wilkin