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:
- Is my assumption correct?
- If so, how come it is possible to make such initialization to a static variable in c++?