C has no special data type for character strings. Instead they are stored as
arrays of characters. Nevertheless, C provides special support for character
arrays that do not extend to other kinds of arrays. For example, C provides
ways to print them (in printf and other output statements) and to read them (in
scanf and other input statements). C also provides a number of library
routines that compute the lengths of strings, append two strings, and the like.
For example, one such C library routine is called strlen. Here is the
header for strlen:
int strlen (char s[]);
As you can see, this function takes an array of characters as an argument and
returns its length as a result. Now think carefully-what is unusual about
this header? Think about how you would go about implementing this function.
Click here for the answer
Most of the C string routines do not take the length of the string as an
argument. This is possible because, by convention, all C strings are assumed
to be terminated by the null character. (The null character is simply the
number 0.) That is, if you think of a string as containing the characters
``Hello'', what it actually looks like is:
_________________________________________________
| | | | | | | | |
| 72 | 101 | 108 | 108 | 111 | 0 | | |
|_____|_____|_____|_____|_____|_____|_____|_____|
That is, the ASCII codes for each of the characters 'H', 'e', 'l', 'l', and 'o'
are stored, followed by the number 0.
So when we ask strlen for the length of a string, we are not asking for
the physical length of the array in which the string is stored. Above, the
physical array has room for 8 characters, but the actual string consists of
only 5 characters. (The null character is not counted as part of the string.)
Why is 0 a good choice for marking the end of a string?
Click here for the answer
If this is such a good idea, why don't we use null-termination for arrays of
all types?
Click here for the answer
Hamlet Project
hamlet@cs.utah.edu