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?