int arr[]; is a conditional definition.
Clause 6.9.2, Clause 2 states:
Declaring an identifier for an object with a file region without an initializer and without a storage class specifier or using a static element of a storage class is a preliminary definition. If a translation unit contains one or more preliminary definitions for an identifier, and a translation unit does not contain an external definition for this identifier, then the behavior is exactly the same as if the translation unit contained a declaration of the scope of this identifier, a composite type at the end of the translation unit, with an initializer equal to 0 .
and example 2 in paragraph 5 of this section clarifies:
If at the end of the translation block containing
int i[];
array i is still of an incomplete type; the implicit initializer forces it to have one element, which is set to zero when the program starts.
So, at the end of the translation unit, your arr array is of type int[1] . To the end, its type is incomplete, so sizeof does not work, since in main the array type is still not complete.
Access to arr[1] causes undefined behavior, since arr has only one element.
source share