My question is whether the behavior of the program is defined or undefined, if it really is at all. If defined, is the value of x known mainly?
This code is definitely not clean, but for me it should work predictably.
int x places the variable in the data segment, which is defined as zero when the program starts. Before main() , static initializers are called. For x this is the code x = x + 1 . x = 0 + 1 = 1 . So main () will return 1.
The code definitely works in an unpredictable way if x is a local variable allocated on the stack. The state of the stack, unlike the data segment, is pretty much guaranteed to contain undefined garbage.
Dummy00001 Jul 22 '10 at 13:12 2010-07-22 13:12
source share