Escape Characters 217
To include the backslash character itself inside a character string, two backslash char-
acters are necessary, so the printfcall
printf ("\t is the horizontal tab character.\n");
displays the following:
\t is the horizontal tab character.
Note that because the \is encountered first in the string, a tab is not displayed in this
case.
To include a double quotation character inside a character string, it must be preceded
by a backslash. So, the printfcall
printf ("\"Hello,\" he said.\n");
results in the display of the message
"Hello," he said.
To assign a single quotation character to a character variable, the backslash character
must be placed before the quotation mark. If cis declared to be a variable of type char,
the statement
c = '\'';
assigns a single quotation character to c.
The backslash character, followed immediately by a ?, is used to represent a ?charac-
ter.This is sometimes necessary when dealing with trigraphsin non-ASCII character sets.
For more details, consult Appendix A, “C Language Summary.”
The final four entries in Table 10.2 enable anycharacter to be included in a character
string. In the escape character '\nnn',nnnis a one- to three-digit octalnumber. In the
escape character '\xnn',nnis a hexadecimal number.These numbers represent the
internal codeof the character.This enables characters that might not be directly available
from the keyboard to be coded into a character string. For example, to include an ASCII
escape character, which has the value octal 33, you could include the sequence \033or
\x1binside your string.
The null character '\0'is a special case of the escape character sequence described in
the preceding paragraph. It represents the character that has a value of 0. In fact, because
the value of the null character is 0 , this knowledge is frequently used by programmers in
tests and loops dealing with variable-length character strings. For example, the loop to
count the length of a character string in the function stringLengthfrom Program 10.2
can also be equivalently coded as follows:
while ( string[count] )
++count;
The value ofstring[count]is nonzero until the null character is reached, at which
point the whileloop is exited.
It should once again be pointed out that these escape characters are only considered a
single character inside a string. So, the character string "\033\"Hello\"\n"actually