Programming in C

(Barry) #1

216 Chapter 10 Character Strings


Escape Characters


As alluded to previously, the backslash character has a special significance that extends
beyond its use in forming the newline and null characters. Just as the backslash and the
letter n, when used in combination, cause subsequent printing to begin on a new line, so
can other characters be combined with the backslash character to perform special func-
tions.These various backslash characters, often referred to as escape characters,are summa-
rized in Table 10.2.

Ta b le 10.2 Escape Characters
Escape Character Name
\a Audible alert
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Ve r tical tab
\\ Backslash
\" Double quotation mark
\' Single quotation mark
\? Question mark
\nnn Octal character value nnn
\unnnn Universal character name
\Unnnnnnnn Universal character name
\xnn Hexadecimal character value nn

The first seven characters listed in Table 10.2 perform the indicated function on most
output devices when they are displayed. The audible alertcharacter,\a, sounds a “bell” in
most terminal windows. So, the printfcall
printf ("\aSYSTEM SHUT DOWN IN 5 MINUTES!!\n");
sounds an alert and displays the indicated message.
Including the backspace character '\b'inside a character string causes the terminal
to backspace one character at the point at which the character appears in the string, pro-
vided that it is supported by the terminal window. Similarly, the function call
printf ("%i\t%i\t%i\n", a, b, c);
displays the value of a, spaces over to the next tab setting (typically set to every eight
columns by default), displays the value of b, spaces over to the next tab setting, and then
displays the value of c.The horizontal tab character is particularly useful for lining up
data in columns.
Free download pdf