Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C For Dummies 2nd Ed 2004.pdf
Скачиваний:
43
Добавлен:
17.08.2013
Размер:
8.34 Mб
Скачать

306 Part IV: C Level

The Old Displaying-Text-with-printf()

Routine

Printf()’s main purpose in life is to display text on the screen. Here is its most basic format:

printf(“text”);

text is the text you want to see on the screen. It’s enclosed in double quotes. The double quotes are enclosed in parentheses, and the entire statement must end with a semicolon.

Special characters — such as a double quote, tab, backspace, and Enter (a new line) — can be included in the text that printf() displays. These characters require the printf() escape sequences, as described in the next section.

printf() can display two or more lines of text by using the \n (new­ line) escape sequence.

To specify a double quote in your text string, use the \” escape sequence.

The printf() Escape Sequences

Table 24-1 lists many of the printf() escape sequences. Most of these you know from using them. Others are very specific, and you may never ever use them in your entire programming life.

Table 24-1

printf() Escape Sequences

Sequence

Shortcut for or Equivalent to

\a

Beeps the speaker

 

 

\b

Backspace (moves the cursor back, no erase)

 

 

\f

Form feed (ejects printer page; may clear the screen on

 

some computers)

 

 

\n

Newline, like pressing the Enter key

 

 

\r

Carriage return (moves the cursor to the beginning of the line)

 

 

 

 

 

Chapter 24: The printf() Chapter 307

 

 

 

 

 

 

 

 

 

Sequence

Shortcut for or Equivalent to

 

 

\t

Tab

 

 

 

 

 

 

\v

Vertical tab (moves the cursor down a line)

 

 

 

 

 

 

\\

The backslash character

 

 

 

 

 

 

\’

The apostrophe

 

 

 

 

 

 

\”

The double-quote character

 

 

 

 

 

 

\?

The question mark

 

 

 

 

 

 

\0

The “null” byte (that’s 0, not the letter O)

 

 

 

 

 

 

\Onn

A character value in octal (base 8)

 

 

 

 

 

 

\xnnn

A character value in hexadecimal (base 16)

 

 

 

 

 

The final two items in Table 24-1 are the most flexible. They allow you to insert any character into any text after you know that character’s octal or hexadecimal code value.

Suppose that you need to display the Escape character in your text. A quick look into Appendix B shows that the hexadecimal code for Escape is 1b. As an escape sequence — an Escape escape sequence — that would be written as

\x1b

Here’s how it would look in printf():

printf(“On some consoles, this clears the screen \x1b[2J”);

You may want to flag this page with a sticky note or dog-ear the corner. The page has stuff no one remembers, so you wind up referring to Table 24-1 often.

The printf() escape-sequence testing program deluxe

To see how some of these characters work, create the PRINTFUN.C program, listed next. You modify the printf() statement at the core of the program to demonstrate how the various escape sequences affect text:

308 Part IV: C Level

/*

printf() escape sequence demonstration program */

#include <stdio.h>

int main()

{

printf(“Here is the \\a sequence: \a”); getchar();

return(0);

}

Enter this program into your text editor. Save it to disk as PRINTFUN.C.

Compile and run PRINTFUN.C. Its purpose is to see how the \a sequence “appears” in the text that’s displayed. Here’s a sample of the program’s output:

Here is the \a sequence: BEEP!

The speaker beeps. How ghastly! Pray that you’re not testing this program in the early afternoon, or else you may wake up your cellmates.

When the program runs, getchar() waits for you to press a key at the keyboard. This pause allows you to examine the output before the pro­ gram quits.

The text string in printf() contains two escape sequences. The first is \\, a double backslash that displays the backslash character. In this example, \\a displays \a — the escape sequence being tested. The second escape sequence is at the end of the string, \a.

Notice that the string in printf doesn’t end in \n. The newline charac­ ter would goof up the display for some of the fancier escape sequences (\r, \t, and \b).

Putting PRINTFUN to the test

The true test of the PRINTFUN program is to reedit it and replace the \\a and \a with another escape sequence. This way, you can test all the sequences to get a feel for what each of them does.

Begin by replacing Line 9 in the program with this one:

printf(“Here is the \\b backspace sequence:\b\b\b\b”);

Chapter 24: The printf() Chapter 309

This line tests the \b, backspace, escape sequence. Save the changes to the program, compile it, and run it.

You see the cursor sitting below the n in sequence when the program runs. That’s because \b backs up the cursor but does not erase.

There are four instances of \b, which back up the cursor four places from the end of the line. (If the cursor isn’t right there, you have a rogue space in the program or you specified more or fewer instances of \b.)

The \n character you’re familiar with, but what does \r do? How is a carriage return different from a new line? Edit Line 9 in PRINTFUN.C to look like this and find out:

printf(“Here is the \\r sequence:\r”);

Save the change to disk, compile, and run.

In the output, you see the cursor flashing under the H at the beginning of the line. The carriage return resembles the carriage return on a typewriter: It moves you to the beginning of the line. It was only by whacking the line-feed bar on a typewriter that the page was advanced.

The \t character produces a tab, like pressing the Tab key. The cursor moves a predefined number of characters to the left. This is good for producing a table in which text has to be lined up. Edit Line 9 in the program to read:

printf(“Able\tBaker\tCharlie\n”);

Then, insert the following lines immediately after the preceding printf() statement:

printf(“1\t2\t3\n”);

printf(“Alpha\tBeta\tGamma\n”);

No spaces are in this printf text string. The words Able, Baker, and Charlie are separated by \t (tab) escape sequences. The line ends with \n, the newline. The same holds true for the two new lines: instances of \t separate the numbers and words.

Double-check your source code! Ensure that you have \t twice in each printf() statement and that \n ends each quoted string of text. Beware of rogue backslashes, which you have a tendency to type as you enter each line. When everything looks okay, save the PRINTFUN.C source code file to disk. Compile it. Run it. Here’s some sample output: