I have the following simple program:
int main()
{
int x = 5;
static int y = x;
return (0);
}
Compiling it with gcc, it produces error for the line static int y = x;
since "initializer element is not constant". I assume this is due to y
being a static variable, whose storage location (data/bss) and initial value need to be known at compile time.
However, when compiling with g++, I don't get any errors and the program runs well (printing y
prints 5).
My questions are:
Your program is well-formed in C++ as local variables with static storage duration are initialized not during startup (with some exceptions for constant expressions; not applicable in this example) but at the first time control passes through their declaration, at which point the initializer expression, containing the local non-static variable x
, is readily available.
Citing cppreference / Storage duration - Static local variables [emphasis mine]
Variables declared at block scope with the specifier static or thread_local (since C++11) have static or thread (since C++11) storage duration but are initialized the first time control passes through their declaration (unless their initialization is zero- or constant-initialization, which can be performed before the block is first entered). On all further calls, the declaration is skipped.
Static initialization in C, however, does not follow the same rules; from cppreference / C language - Initialization:
When initializing an object of static or thread-local storage duration, every expression in the initializer must be a constant expression or string literal.
Thus your program is ill-formed in C.