This example outputs the word correctly, without the garbage characters. It does so because cout looks for the
NULL character and stops output at that point:
void main(void)
{
char word[10];
cout << "Enter a string ";
cin >> word;
cout << word;
}
Output:
Celsius
- The difference between a string and an ordinary array of characters is the terminating NULL character
- Strings are said to be "NULL terminated", because they end with a the NULL character.
- If there is no NULL terminator, cout will continue to output characters until it "stumbles" upon a NULL:
char word[7] = {'C','e','l','s','i','u','s'};
cout << word;
Output: (potential)
CelsiusÉî!? Q?Éî ! Yæ?SX LASTDRIVEL
MULTITRACKDRIVPARMP?STACKSKJ \æ\æ;æ? `?` ` `æ