Previous: 4.1.3 Character I/O Using getchar() and putchar()
Up: 4.1 A New Data Type: char
Previous Page: 4.1.3 Character I/O Using getchar() and putchar()
Next Page: 4.2 Sample Character Processing Functions
Frequently, we have needed to write constants that are not single characters
but are sequences of characters. A sequence of zero or more characters is
called a string of characters or simply a string.
We have already used strings as
arguments in function calls to printf() and scanf().
In C, there is no primitive
data type for strings; however, as a convenience, string constants
(also called string literals)
may be written directly into a program
using double quotes. The double quotes are not part of a string
constant; they are merely used to delimit (define the limits,) of the string
constant.
(To include a double quote as part of a string, escape the double quote
with the character).
"This is a string constant." "This string constant includes newline character.\n" "This string constant includes \" double quotes."Escape sequences may of course be included in string constants. A string constant may even contain zero characters, i.e. an empty string:
""Such a string is also called a null string.
Two adjacent strings are concatenated at compile time. Thus,
"John " "Doe"are equivalent to:
"John Doe"Whenever a string constant appears in a source program, the compiler stores the sequence of characters in contiguous memory locations and appends a NULL character to indicate the end of the string (see Figure 4.6). The compiler then replaces the string constant by the address where the characters are stored. Observe that a string of a single character is different from a character constant. Thus, 'c' is a character constant; but, "c" is a string constant consisting of one character and the NULL character, as seen in the figure.
As we have said,
a character constant takes on its ASCII value. The value of a string constant
is the address where the string is stored. How this value can be used will be
discussed in Chapter .
For now, think of a string constant as a convenient
representation, the exact nature of which will become clear later.