Sams Teach Yourself C in 21 Days

(singke) #1
Working with the Screen, Printer, and Keyboard 365

14


As explained on Day 7, the printf()format string can contain escape sequences that
provide special control over the output. Table 14.6 lists the most frequently used escape
sequences. For example, including the newline sequence (\n) in a format string causes
subsequent output to appear starting on the next screen line.

TABLE14.6 The most frequently used escape sequences
Sequence Meaning
\a Bell (alert)
\b Backspace
\n Newline
\t Horizontal tab
\\ Backslash
\? Question mark
\’ Single quote
\” Double quote

printf()is somewhat complicated. The best way to learn how to use it is to look at
examples and then experiment on your own. Listing 14.13 demonstrates many of the
ways you can use printf().

LISTING14.13 printf.c. Some ways to use the printf()function
1: /* Demonstration of printf(). */
2:
3: #include <stdio.h>
4:
5: char *m1 = “Binary”;
6: char *m2 = “Decimal”;
7: char *m3 = “Octal”;
8: char *m4 = “Hexadecimal”;
9:
10: int main( void )
11: {
12: float d1 = 10000.123;
13: int n, f;
14:
15:
16: puts(“Outputting a number with different field widths.\n”);
17:
18: printf(“%5f\n”, d1);
19: printf(“%10f\n”, d1);
20: printf(“%15f\n”, d1);
21: printf(“%20f\n”, d1);

22 448201x-CH14 8/13/02 11:12 AM Page 365

Free download pdf