Why is this valid C
Solution 1:
The excess elements are just ignored. There are two parts of 6.7.8 Initialization that you care about. First, from paragraph 17:
Each brace-enclosed initializer list has an associated current object. When no designations are present, subobjects of the current object are initialized in order according to the type of the current object: array elements in increasing subscript order, structure members in declaration order, and the first named member of a union.
That one explains why you get 1, 7, and 9 - the current object gets set by those braces. Then as to why it doesn't care about the extras, from paragraph 20:
... only enough initializers from the list are taken to account for the elements or members of the subaggregate or the first member of the contained union; any remaining initializers are left to initialize the next element or member of the aggregate of which the current subaggregate or contained union is a part.
Solution 2:
int a[3] = { { {1, 2}, {3, 4}, 5, 6 }, {7, 8}, {9}, 10 };
is invalid.
It is invalid for the same reasons int b[1] = {1, 2};
is invalid: because C99 says
(C99, 6.7.8p1) "No initializer shall attempt to provide a value for an object not contained within the entity being initialized."
The last element 10
in a
initializers attempts to provide a value for an object not contained within the entity being initialized.