Discussion: String or Not?
Explore the fundamental difference between character arrays and strings in C programming. Understand why null termination is essential for defining strings and how omitting it can lead to unpredictable program output or errors.
We'll cover the following...
Run the code
Now, it's time to execute the code and observe the output.
#include <stdio.h>
int main()
{
char nonstring[] = {
'g', 'r', 'e', 'e', 't',
'i', 'n', 'g', 's', ',', ' ', 'h', 'u', 'm', 'a', 'n'
};
char data[] = { 127, 129, 255 };
printf("%s\n", nonstring);
return(0);
}Understanding the output
If you’re lucky, the output appears like this:
greetings, human0?u??
Note: Garbage values can vary from one compiler to another.
If you’re unlucky, you see far more garbage spew all over the terminal window. In some rare cases you may see nothing, depending on the code page or font used for the terminal window.
A character array is not a string
Character array nonstring[] is a collection of single characters. It’s not a string. The final character in the array is ‘n’ and not the null character, ‘\0’, which terminates a string.
When the printf() statement outputs the array as a string, it assumes that the string terminates with the null character. Because character array nonstring[] is unterminated, the printf() function continues to gobble characters, chewing along until it finds an accidental null character in the soupy garbage existing in memory. In the output above, that happenstance occurs after five random characters are output.
Note: Always terminate strings.
A character array is not a string. Only with the null character acting as a caboose does a collection of characters become a string. Remembering this admonition is vital when we create and manipulate our own strings. Many computer errors and security vulnerabilities exist due to improperly formed strings in C programming.
Nerdy tidbits
The non-terminating non-string may output properly in some cases, though don’t ever count on it. Most computer systems today allocate storage in 16-byte chunks. If a non-terminated string is less than this size, it may sit in a 16-byte chunk already filled with zeros, which terminates the string accidentally. This condition isn’t anything you can take to the bank, but it may explain why some non-terminated strings work under these conditions.
In this above code, the nonstring[] array contains exactly 16 characters, which we set on purpose so that it would fill a 16-byte memory chunk. The 16-byte boundary increases the chances that the next 16-byte chunk contains garbage or other information, which is output with the unterminated string.
Adding the data[] the array also helps fill the next 16-byte chunk with preset “garbage.”
Note: The point to drive home is that a character array isn’t a string unless the characters are terminated with the null character,
\0.