Let's consider the following example:
int main()
{
char some_string[] = "12345678";
printf("%zu", sizeof(some_string));
return 0;
}
Output
9
...Program finished with exit code 0
Press ENTER to exit console.`
The above is correct. The size of the some_string
var is 8 characters + the NUL byte = 9
Now if we change it a little bit to:
int main()
{
char some_string[8] = "12345678";
printf("%zu", sizeof(some_string));
return 0;
}
it prints:
8
...Program finished with exit code 0
Press ENTER to exit console.`
Why is the latter case ignoring the NUL byte?
In C, when a size is provided for the array in the definition, the array is that size, regardless of the initializer. A string literal of that size or smaller may be used to initialize a character array, and a string literal that is exactly that size excluding the null character may be used to initialize the array. C 2018 6.7.9 14 says:
An array of character type may be initialized by a character string literal or UTF–8 string literal, optionally enclosed in braces. Successive bytes of the 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.
This is allowed because sometimes arrays of characters are used as plain data (for example, as look-up tables) rather than as null-terminated strings, but it may be convenient to use a string literal to initialize them.