Note
Appendix A, “The ASCII Table,” contains an ASCII table (first mentioned in Chapter
2 , “Writing Your First C Program”). The very first entry is labeled null, and the ASCII
number for null is 0. Look further down at ASCII 48, and you’ll see a 0. ASCII 48 is
the character '0', whereas the first ASCII value is the null zero. C puts the null zero
at the end of strings. Even the string "I am 20" ends in an ASCII 0 directly after the
character 0 in 20.
The string terminator is sometimes called \0 (backslash zero) because you can represent the null
zero by enclosing \0 in single quotes. Therefore, ' 0 ' is the character zero, and '\0' is the string
terminator. (Remember the escape sequences covered in Chapter 4, “Your World Premiere—Putting
Your Program’s Results Up on the Screen,” that were also single characters represented by two
characters—a backslash followed by a letter or another character. Now you have a backslash number
to add to the collection.)
Figure 6.1 shows how the string "Crazy" is stored in memory. As you can see, it takes 6 bytes (a
byte is a single memory location) to store the string, even though the string has only five letters. The
null zero that is part of the string "Crazy" takes one of those six memory locations.
FIGURE 6.1 A string always ends with a null zero in memory.
The Length of Strings
The length of a string is always the number of characters up to, but not including, the null zero.
Sometimes you will need to find the length of a string. The null zero is never counted when
determining the length of a string. Even though the null zero must terminate the string (so that C knows
where the string ends), the null zero is not part of the string length.
Given the definition of the string length, the following strings all have lengths of nine characters:
Wednesday
August 10
I am here
When counting the length of strings, remember that you must account for every space. So although the
second string has eight letters and numbers, as well as a space in the middle, and the third string has
seven letters, as well as two spaces in the middle, are all considered nine-character strings. If you
chose to put three spaces between August and 10 in the middle example, it would become an 11-
character string.