[No!] Web doc bug: adding \0 to char array??

char Str2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};

According to the C standard (C++ also):

If you declare an array with a known size and you give it an initializer list with fewer elements than the size of the array, the remaining elements will be set to zero. (So Str2[7] is set to a zero byte, '\0'.)

char Str7[7] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};

Should the compiler add a \0 to this? Obviously not.

Since the number of elements in the initializer list is equal to the number of elements that you declared, the compiler will fill the array exactly as you told it to.

Regards,

Dave

Footnote:

In the C language standard document ISO/IEC 9899

Section 6.7.8, paragraph 19.

[color=#0000ff]If there are fewer initializers in a brace-enclosed list than there are elements or members
of an aggregate, or fewer characters in a string literal used to initialize an array of known
size than there are elements in the array, the remainder of the aggregate shall be
initialized implicitly the same as objects that have static storage duration.[/color]

Note that this is one of the very, very few things in Standard C that is different from the language description in the Second Edition of K&R.

The language is more complicated for C++ (since it is concerned with classes and not just simple arrays), but the outcome is the same.

The bottom line for me is that, as a matter of style, I would (probably) not initialize a C-style "string" by declaring an array like this and giving it an initializer list with fewer characters than the size of the array. Nevertheless, it is not "wrong" to do so, according to the language standards documents. Whether it is a "good idea" to encourage new programmers to get into the habit of engaging in such practices, well...