Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

variable zero-initialization - undefined behaviour or not [duplicate]

Tags:

c++

c

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?

like image 482
FrozenHeart Avatar asked Jul 28 '26 13:07

FrozenHeart


1 Answers

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).

like image 112
AnT Avatar answered Jul 31 '26 02:07

AnT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!