简体   繁体   中英

Why doesn't sizeof() doesn't include the NUL byte in this example?

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.

In contrast to C++, in C, it is permissible to initialize a character array using a string literal in such a way that it does not contain a null terminating character, by limiting the length of the array so that it is the length of the string without the null character.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM