I am a little confused by the following snippets of C code:
printf("Peter string is %d bytes\n", sizeof("Peter"));
This tells me that when C compiles the string in double quotes, it will automatically add an extra byte for the null terminator.
printf("Hello '%s'\n", "Peter");
The function printfknows when to stop reading the string "Peter" because it reaches the terminator zero, so ...
char myString[2][9] = {"123456789", "123456789" };
printf("myString: %s\n", myString[0]);
It printfdisplays all 18 characters, because there are no null terminators (and they would not fit without removing the 9s). Does C add a null terminator in the variable definition?
source
share