The presence of excess initializers violates a constraint in C 2018 6.7.9 2:
No initializer shall attempt to provide a value for an object not contained within the entity being initialized.
'k'
and 's'
would provide initial values for a2[4]
and a2[5]
. Since a2[4]
or a2[5]
do not exist, they are not contained within a2
, and the constraint is violated.
That said, compilers will typically provide a warning then ignore the excess initializers and continue. This is the least of the problems in the code you show and has no effect on the output you see.
After the definition of a2
, you print it using %s
. %s
requires a pointer to the first character in a sequence of characters terminated by a null. However, there is no null character in a2
. The resulting behavior is not defined by the C standard. Often, what happens is a program will continue to print characters from memory beyond the array. This is of course not guaranteed and is especially unreliable in the modern high-optimization environment.
Assuming the printf
does continue to print characters beyond the array, it appears that, on one system, there happens to be a null character beyond the array, so the printf
stops after four characters. When you later print a2[4]
(also behavior not defined by the C standard) as an integer (%d
) and a character (%c
), we see there is indeed a null character there.
On the other system, there is a −1 value in the memory at a2[4]
, which displays as “�”. After it, there are presumably some number (possibly zero) of non-printing characters and a null character.
Additionally, you print sizeof(a2)
using the printf
specifier %u
. This is incorrect and may have undefined behavior. A proper specifier for the result of sizeof
is %zu
.