0

I cannot get the reason behind being unable to initialize static variables withOUT constant literals.

This initialization is perfectly fine, it compiles :

#include <stdio.h>

int main ()
{
    static int i = 15;
}

While this one isn't, and doesn't compile :

#include <stdio.h>

int initializer ()
{
    return 15;
}

int main ()
{
    static int i = initializer();
}

And neither does this one compile :

#include <stdio.h>


int main ()
{
    int x = 15;
    static int i = x;
}

For the last 2 programs the compiler gives this error :

Initializer element is not constant.

When I researched the reason for this, I came up with two explanations; one from https://www.geeksforgeeks.org/g-fact-80/ and the other from C99 Standard 5.1.2 Execution Environments.

GeeksforGeeks states "All objects with static storage duration must be initialized (set to their initial values) before execution of main() starts. So a value which is not known at translation time cannot be used for initialization of static variables."

C99 Standard states "All objects with static storage duration shall be initialized (set to their initial values) before program startup."

But what do these sentences really mean? And what do these have to do with constant literals? When I declare a static variable inside main() function as the first code piece it compiles, and because the declaration is inside main() the execution of main() has already started. So the initialization takes place AFTER "the execution of main starts" , in contrast to GeeksforGeeks' statement.

  • 1
    "because the declaration is inside main() the execution of main() has already started" - you'd think so, but nope. – user2357112 Aug 02 '20 at 08:53
  • _"When I declare a static variable inside main() function as the first code piece it compiles"_ - Incorrect, that's only true when using constants, as your code examples have already proven. – wovano Aug 02 '20 at 08:54
  • _"...and because the declaration is inside main() the execution of main() has already started."_ - Also incorrect (already stated by user2357112 too), since static variables are determined at compile time. – wovano Aug 02 '20 at 08:55
  • This code would be legal in C++, so one option for you is to compile as C++ even if you keep writing C for the most part. – John Zwinck Aug 02 '20 at 08:59
  • @wovano the first code piece actually compiles? And the first one is already using the constant literal ´15´ . I didn't get your first comment. But yes, if static variables are determined at compile time it clarifies everything. Also I just found out that we cannot initialize a static variable with another static variable. Maybe you know the reason? – nowayicandothis Aug 02 '20 at 09:03
  • @nowayicandothis A static variable is not a constant expression. It needs a constant expression to get initialised, but it isn't one by itself. – n. m. could be an AI Aug 02 '20 at 09:06
  • @n.'pronouns'm. a static variable is not a constant but they are both determined at compile time so wouldn't it be sensible to initialize one static with another? – nowayicandothis Aug 02 '20 at 09:12
  • `static` is a `compile-time constant` not a `const`. most compilers allow to set static with formulas where each part must be compile-time constant also, no function calls possible there - functions become pointers and their address is only known at run-time – Ol Sen Aug 02 '20 at 09:13
  • 1
    C is deliberately kept simple. `1` good, `i` bad. – n. m. could be an AI Aug 02 '20 at 09:21

0 Answers0