char string0[]= "0123456789"; // sizeof(string0)=11
char string1[11]="0123456789A"; // sizeof(string1)=11
The string1 case is clear, allocated 11 Bytes for chars plus one for ” and sizeof() do not count terminator.
The string0 case is not clear to me, allocated 10 Bytes for chars plus one for ”, so sizeof() should report 10
Why sizeof() say the same size in both cases?
7
sizeof() do not count terminator”
sizeof
reports the size of the allocated memory. There is no counting involved. You might be mistaking sizeof
for strlen
, which indeed does not count the string terminator.
The first one is auto-allocated, and is size 11 because you have 10 characters and NUL. The second is manually allocated to size 11, which is filled with 11 characters, with no NUL since there is no space for it; if you try to print it (or indeed apply strlen
on it), you will get gibberish at best.
See Why does gcc allow char array initialization with string literal larger than array?
1
The sizeof
operator returns the number bytes needed to store a variable (or a type). For string0
the compiler deduces that you have sizeof "0123456789"
which is indeed 11 byte string literal including the trailing ''
.
char string1[11]
tells the compiler to allocate 11 bytes. As you initialize the variable to a 12 byte string literal only the first 11 bytes are copied to the variable so it’s actually an array of bytes (without the trailing ''
) and not a string.
Here is what C 2023 6.7.10 §10 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.
OK, than you all, now is clear.
I post some code to show this behave:
#include <stdio.h>
char string0[]; // space for '', initialized
char string1[10]; // space for 10 Bytes, initialized
char string2[10]=""; // space for 10 Bytes, initialized
char string3[]=""; // space for '', initialized
char string4[]= "0123456789"; // space for 11 Bytes, 10 plus ''
char string5[11]="0123456789A"; // space for 11 Bytes, 11 char, miss ''
char string6[10]="0123456789"; // space for 10 Bytes, 10 char, miss ''
char* string7; // space for sizeof(ptr), not initialized
char* string8=NULL; // space for sizeof(ptr), initialized to NULL
char* string9=string4; // space for sizeof(ptr), initialized
//stringX; // same as &stringX[0]
int main() {
printf("size:%02ld string0:'0123456789A'n", sizeof("0123456789A")); // 12 '0123456789A'
printf("size:%02ld string1:'%s'n", sizeof(string1), string1); // 10 ''
printf("size:%02ld string2:'%s'n", sizeof(string2), string2); // 10 ''
printf("size:%02ld string3:'%s'n", sizeof(string3), string3); // 1 ''
printf("size:%02ld string4:'%s'n", sizeof(string4), string4); // 11 '0123456789'
printf("size:%02ld string5:'%s'n", sizeof(string5), string5); // 11 '0123456789A'
printf("size:%02ld string6:'%s'n", sizeof(string6), string6); // 10 '0123456789'
printf("size:%02ld string7:'%s'n", sizeof(string7), string7); // 8 NULL
printf("size:%02ld string8:'%s'n", sizeof(string8), string8); // 8 NULL
printf("size:%02ld string9:'%s'n", sizeof(string9), string9); // 8 '0123456789'
printf("string4[9] : %02d '%c'n", string4[9] , string4[9]); // '9'
printf("string4[10]: %02d '%c'n", string4[10], string4[10]); // ''
printf("string5[9] : %02d '%c'n", string5[9] , string5[9]); // '9'
printf("string5[10]: %02d '%c'n", string5[10], string5[10]); // 'A'
printf("string5[11]: %02d '%c'n", *(string5+11), *(string5+11)); // '' confirm NULL
printf("string5[12]: %02d '%c'n", *(string5+12), *(string5+12)); // ''
printf("string6[9] : %02d '%c'n", string6[9] , string6[9]); // '9'
printf("string6[10]: %02d '%c'n", *(string6+10), *(string6+10)); // '' confirm NULL
printf("string6[11]: %02d '%c'n", *(string6+11), *(string6+11)); // ''
printf("string4 addr:%pn", string4);
printf("string5 addr:%pn", string5);
printf("string6 addr:%pn", string6);
return 0;
}