It is not necessarily undefined behaviour, because it depends very much upon the version of the language — things are much clearer in C++14, however. Either way though, there are no guarantees you will get the value 1.
int i;
in this context declares i
with automatic storage duration, and with default initialization. For int
, default initialization leaves the variable with indeterminate value.
- In C, indeterminate value is defined in the standard:
i
will have some value, or a trap representation.
- The increment operation adds one to the value of
i
, which was indeterminate. If it happened to be a trap representation, this could raise a signal. (On your typical PC hardware and C or C++ compiler, there won't be any trap representations for int
.)
- Until the C++14 draft, C++ left open what was meant by indeterminate; it was used, but not defined. Once might assume that it meant the same thing as in the C standard, but frankly there are no guarantees here.
- With C++14, this has been nailed down, and except for a limited number of operations on narrow characters, doing anything with indeterminate values leads to undefined behaviour.
In short: if it were C, it's not undefined behaviour, but no promises that it will be 1; if it were pre-C++14 C++, it's not properly specified in the standard, but at the very least, there are no promises that it will be 1; and in C++14 it is specified that this is undefined behaviour.
There's an interesting and informative discussion of what indeterminate means in C++ on stackoverflow.