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

310 Part IV: C Level

Able

Baker

Charlie

1

2

3

Alpha

Beta

Gamma

Though the \ts in the printf statements look sloppy, the output is definitely organized. Tabular, dude!

The “tab stops” are preset to every eighth column in C’s output. Using a \t inserts a given number of space characters in the output, lining up the next bit of text at the next tab stop. I mention this because some people assume that the tab always moves over eight (or however many) characters. That is not the case.

The \f and \v characters display special symbols at the Windows com­ mand prompt. Rather than a form feed, \f displays the ankh character. Rather than a vertical tab, \v displays the male symbol.

As long as you know a character’s hexadecimal code value, you can always get it displayed by using the \x escape sequence. Just plug in the hexadecimal code and there you go!

The Complex printf() Format

The printf() function can also be used to display the contents of variables, which you have been seeing throughout this book with integer variables and the %d placeholder, character variables and %c, and so on. To make it happen, printf() uses this format:

printf(“format_string”[,var[,...]]);

Text still appears in double quotes, but after it’s used to display the values in variables, it becomes a format string. (It’s still the same text in double quotes.) The format string is followed by one or more variables, var.

Those var variables are plugged in to appropriate spots in the format_string according to special percent-sign placeholders. Those percent-sign place­ holders are called conversion characters. For example:

printf(“Yeah, I think %s is a jerk, too.\n”,jerk);

The format string is text that printf() displays on the screen: Yeah, I think ____ is a jerk, too. The %s is a conversion character — a blank — that must be filled by a string of text. (I call them placeholders, but the lords of C claim that they’re conversion characters.)

Chapter 24: The printf() Chapter 311

After the format string is a comma and then jerk. The jerk is a string vari­ able whose contents replace the %s in printf()’s output.

You can specify any number of conversion characters in printf()’s format string. Each conversion character, however, must have a cor­ responding variable; three %s characters would require three string variables.

Yeah, this works like fill-in-the-blanks; the % conversion characters are the blanks.

You can specify both strings of text and numbers by using the proper conversion characters, as described in the next section.

Refer to Figure 4-2, in Chapter 4, for an illustration of how the conver­ sion characters work with variables in a printf() statement.

The printf() Conversion Characters

Table 24-2 lists all the printf() conversion characters in the known universe — even those you haven’t seen before and some you may never see again.

Table 24-2

The printf() Conversion Characters

Conversion Character

Displays Argument (Variable’s Contents) As

%c

Single character

 

 

%d

Signed decimal integer (int)

 

 

%e

Signed floating-point value in E notation

 

 

%f

Signed floating-point value (float)

 

 

%g

Signed value in %e or %f format, whichever is shorter

 

 

%i

Signed decimal integer (int)

 

 

%o

Unsigned octal (base 8) integer (int)

 

 

%s

String of text

 

 

%u

Unsigned decimal integer (int)

 

 

%x

Unsigned hexadecimal (base 16) integer (int)

 

 

312 Part IV: C Level

Additional formatting stuff

Your C compiler’s documentation should contain a list of additional printf() formatting infor­ mation, bonus characters that can be used in conjunction with the conversion characters to additionally format printf()’s output. That information is too complex and detailed to list here for every compiler. Instead, look up printf in your online reference manual and note these formatting sections:

Flags

Width specifiers

Precision specifiers

Input-size modifiers

You don’t need this information now for under­ standing the C programming language. However, it comes in handy as you begin working with numbers or require a little fancier output than what you have done with printf() in this chapter.

In addition to the conversion characters in Table 24-2, three other characters exist: %p, %n and %%. The %p and %n are advanced conversion characters, beyond the scope of this book. The %% merely prints a % on the screen.

As with the escape sequences, the conversion characters are something you use often but never remember. I advise tacking a sticky note to this page for future reference.

The %x, %e, and %g conversion characters also have uppercase equiva­ lents: %X, %E and %G. By using capital letters rather than lowercase, you ensure that any letters in the output are all listed as uppercase. For exam­ ple, %x would display a hexadecimal value as 1ba2, but %X would display 1BA2. Otherwise, the conversion character’s behavior is the same.

%p is used to print a value as a pointer. Pointers are covered in this book’s companion, C All-in-One Desk Reference For Dummies (Wiley).