No compiler error when fixed size char array is initialized without enough room for null terminator

This is expected behavior for line 6, from the draft C99 standard section 6.7.8 Initialization paragraph 14 says (emphasis mine):

An array of character type may be initialized by a character string
literal, optionally enclosed in braces. Successive characters of the
character string literal (including the terminating null character
if there is room or if the array is of unknown size
) initialize the
elements of the array.

In the C11 draft standard the relevant section with similar wording is 6.7.9 paragraph 14, and as the C FAQ says:

The array is therefore not a true C string and cannot be used with
strcpy, printf’s %s format, etc.

As Keith Thompson noted, C++ is stricter, the relevant section in the draft C++ standard says the following:

There shall not be more initializers than there are array elements. [ Example:

char cv[4] = "asdf"; // error

is ill-formed since there is no space for the implied trailing ’\0’. —end example ]

Leave a Comment