initializing static variable with a function call gives compilation error?

后端 未结 3 728
醉话见心
醉话见心 2020-12-19 04:52
#include 
int foo(){
    return 1;
}
int main(void) {
    static int q = foo(); 
    return 0;
}

Here is a link for the same. This i

3条回答
  •  天涯浪人
    2020-12-19 05:16

    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.

提交回复
热议问题