by bobsc123 · 8y ago · 0 comments
Null termination is part of the C language.
Example: char str[]= "1234";
printf ("%s: %lu", str, sizeof(str));
Prints:
1234: 5A couple of ugly corner cases:
const char str[] = "abcd\0efgh";
printf("length = %zu, size = %zu, value = \"%s\"\n",
strlen(str), sizeof str, str);
output: length = 4, size = 10, value = "abcd"
And: const char str[4] = "abcd";
printf("length = %zu, size = %zu, value = \"%s\"\n",
strlen(str), sizeof str, str);
This has undefined behavior. (Which is a good reason to let the compiler figure out how big the array has to be. Computers are better at counting things than you are. Let them.)This explicitly is a different kind of representation in memory.