characters \ and a actually being printed.
You will see a lot of escape sequences in printf() functions. Any time you want to “move down”
to the next line when printing lines of text, you must type \n so that C produces a newline, which
moves the blinking cursor down to the next line on the screen. The following printf() statements
print their messages on separate lines because of the \n at the end of the first one:
printf("Write code\n");
printf("Learn C");
Tip
The \n could have been placed at the beginning of the second line, and the same output
would have occurred. Because escape sequences are characters to C, you must enclose
them in quotation marks so that C knows that the escape sequences are part of the string
being printed. The following also produces two lines of output:
printf("Write code\nLearn C");
Double quotation marks begin and end a string, single quotation marks begin and end a character, and
a backslash signals the start of an escape sequence, so they have their own escape sequences if you
need to print them. \a rings your computer’s bell, \b moves the cursor back a line, and \t causes the
output to appear moved over a few spaces. There are other escape sequences, but for now, these are
the ones you are most likely to use.
The following program listing demonstrates the use of all the escape sequences listed in Table 4.1.
As always, your best bet is to try this program and then tweak it to something you’d like to try:
Click here to view code image
// Absolute Beginner's Guide to C, 3rd Edition
// Chapter 4 Example 1--Chapter4ex1.c
#include <stdio.h>
main()
{
/* These three lines show you how to use the most popular Escape
Sequences */
printf("Column A\tColumn B\tColumn C");
printf("\nMy Computer\'s Beep Sounds Like This: \a!\n");
printf("\"Letz\bs fix that typo and then show the backslash ");
printf("character \\\" she said\n");
return 0;
}
After you enter, compile, and run this code, you get the following results:
Click here to view code image