Why must a char array end with a null character?

arrays, c, c++, char, null-terminated

Solution

In C, if you have a pointer to an array, then there is not way to determine the length of that array. As @AProgrammer points out, the designers could have left it at that and forced the programmer to keep track of the length of all character arrays. However, that would have made text processing in C even harder than it already is.

Therefore the language designers settled on a convention that would allow string length to be inferred by the presence of a null character indicating the end of the string.

For example, consider `strcpy`:

char *strcpy(char *destination, const char *source);

There is no way in C to determine the length of the array that the pointers `destination` and `source` point to. So, without the presence of a sentinel value to indicate the end of the string, the only other solution would have been to pass extra parameters indicating the length of the `source` string.

Of course, in light of modern security considerations, string processing functions that receive buffer length parameters have been introduced. But the computing landscape looked very different at the time that the null-terminated string was invented.

Problem

Why does a char `array` have to end with a null character? Is there any reason that I have to add the null character to to every `char` array ? It seems that they get treated the same way.

Original source