Why is this non-null terminated string printed correctly
str2[100] = {'a'};
does not fill str2
with 100 repeated a. It just sets str[0] to 'a' and the rest to zero.
As far back as C89:
3.5.7 Initialization
...
Semantics
...
If an object that has static storage duration is not initialized explicitly, it is initialized implicitly as if every member that has arithmetic type were assigned 0 and every member that has pointer type were assigned a null pointer constant. If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate./65/
...
If there are fewer initializers in a list than there are members of an aggregate, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
First, the rule of initialization for aggregate types[1], quoting C11
, chapter 6.7.9 (emphasis mine)
The initialization shall occur in initializer list order, each initializer provided for a particular subobject overriding any previously listed initializer for the same subobject;151) all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration.
and,
If an object that has static or thread storage duration is not initialized explicitly, then:
if it has pointer type, it is initialized to a null pointer;
if it has arithmetic type, it is initialized to (positive or unsigned) zero;
if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;
if it is a union, the first named member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;
Now, an initialization statement like
char str2[100] = {'a'};
will initialize str2[0]
to 'a'
, and str2[1]
through str2[99]
with 0
, according to the above rule. That 0
value is the null-terminator for strings.
Thus, any value you store there, lesser than the length of the array, up to the length-1
element, is automatically going to be terminated by a null.
So, you're okay to use the array as string and get the expected behavior of that of a string.
[1]: Aggregate types:
According to chapter 6.2.5/P21
[...] Array and structure types are collectively called aggregate types.