Possible Duplicate:
Is this self initialization valid?
Is this a well-defined C/C++ program or not?
int foo = foo;
int main()
{
}
Would foo
be zero-initialized, or is it undefined behaviour?
Possible Duplicate:
Is this self initialization valid?
Is this a well-defined C/C++ program or not?
int foo = foo;
int main()
{
}
Would foo
be zero-initialized, or is it undefined behaviour?
It is an ill-formed C program. In C initializers for objects with static storage duration must be constant expressions. The foo
on the right-hand side is not a constant expression.
In C++ it is well-formed and has defined behavior, because of zero-initialization of objects with static storage duration (which takes place before any other initialization).
It doesn't even compile in C. You cannot initialize global variables other than using compile time constants.
Static/global variables are initialized with 0. Thus:
int ThisIsZero;
int main(void)
{
static int AndSoIsThis;
int ButThisIsNotInitialized;
...
};
That does not compile - and what whats the point of the question?