* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / c.programming.notes / sx6a.html
blob78b81fe94a9bc8cd4287deb6ca761783406a5d74
1 <!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995-7 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
5 <html>
6 <head>
7 <link rev="owner" href="mailto:scs@eskimo.com">
8 <link rev="made" href="mailto:scs@eskimo.com">
9 <title>6.1 <TT>printf</TT></title>
10 <link href="sx6.html" rev=precedes>
11 <link href="sx6b.html" rel=precedes>
12 <link href="sx6.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>6.1 <TT>printf</TT></H2>
17 <p><TT>printf</TT>'s name comes from <B>print</B> <B>f</B>ormatted.
18 It generates output under the control of a <dfn>format string</dfn>
19 (its first argument)
20 which consists of literal characters to be printed
21 and also
22 special character sequences--<dfn>format specifiers</dfn>--which
23 request that
24 other arguments be fetched, formatted, and inserted into the string.
26 Our very first program was nothing more than a call to <TT>printf</TT>,
27 printing a constant string:
28 <pre>
29 printf("Hello, world!\n");
30 </pre>
31 Our second program also featured a call to <TT>printf</TT>:
32 <pre>
33 printf("i is %d\n", i);
34 </pre>
35 In that case, whenever <TT>printf</TT>
36 ``printed''
37 the string
38 <TT>"i is %d"</TT>,
39 it did not print it verbatim;
40 it replaced the two characters <TT>%d</TT> with the value of the
41 variable <TT>i</TT>.
42 </p><p>There are quite a number of format specifiers for <TT>printf</TT>.
43 Here are the
44 basic
45 ones
47 <br>
48 <br>
49 <pre>
50 <TT>%d</TT> print an <TT>int</TT> argument in decimal
51 <TT>%ld</TT> print a <TT>long int</TT> argument in decimal
52 <TT>%c</TT> print a character
53 <TT>%s</TT> print a string
54 <TT>%f</TT> print a <TT>float</TT> or <TT>double</TT> argument
55 <TT>%e</TT> same as <TT>%f</TT>, but use exponential notation
56 <TT>%g</TT> use <TT>%e</TT> or <TT>%f</TT>, whichever is better
57 <TT>%o</TT> print an <TT>int</TT> argument in octal (base 8)
58 <TT>%x</TT> print an <TT>int</TT> argument in hexadecimal (base 16)
59 <TT>%%</TT> print a single <TT>%</TT>
60 </pre>
61 <br>
62 <br>
63 It is also possible to specify the width and precision of
64 numbers and strings as they are inserted
67 (somewhat like FORTRAN <TT>format</TT> statements);
68 we'll present those details in a later chapter.
69 (Very briefly, for those who are curious:
70 a notation like <TT>%3d</TT> means to print an <TT>int</TT>
71 in a field at least 3 spaces wide;
72 a notation like <TT>%5.2f</TT> means to print a <TT>float</TT> or <TT>double</TT>
73 in a field at least 5 spaces wide,
74 with two places to the right of the decimal.)
75 </p><p>To illustrate with a few more examples:
76 the call
77 <pre>
78 printf("%c %d %f %e %s %d%%\n", '1', 2, 3.14, 56000000., "eight", 9);
79 </pre>
80 would print
81 <pre>
82 1 2 3.140000 5.600000e+07 eight 9%
83 </pre>
84 The call
85 <pre>
86 printf("%d %o %x\n", 100, 100, 100);
87 </pre>
88 would print
89 <pre>
90 100 144 64
91 </pre>
92 Successive calls to <TT>printf</TT> just build up the output a
93 piece at a time, so the calls
94 <pre>
95 printf("Hello, ");
96 printf("world!\n");
97 </pre>
98 would also print <TT>Hello, world!</TT>
99 (on one line of output).
100 </p><p>Earlier we learned
102 that C represents characters internally as
103 small integers corresponding to the characters' values in the
104 machine's character set (typically ASCII).
105 This means that there isn't really much difference between a
106 character and an integer in C; most of the difference is in
107 whether we choose to interpret an integer as an integer or a
108 character.
109 <TT>printf</TT> is one place where we get to make that choice:
110 <TT>%d</TT> prints an integer value as a string of digits representing
111 its decimal value,
112 while <TT>%c</TT> prints the character corresponding to a character set value.
113 So the lines
114 <pre>
115 char c = 'A';
116 int i = 97;
117 printf("c = %c, i = %d\n", c, i);
118 </pre>
119 would print <TT>c</TT> as the character A and <TT>i</TT> as the number 97.
120 But if, on the other hand, we called
121 <pre>
122 printf("c = %d, i = %c\n", c, i);
123 </pre>
124 we'd see the decimal value (printed by <TT>%d</TT>) of the character
125 <TT>'A'</TT>, followed by the character (whatever it is) which happens
126 to have the decimal value 97.
127 </p><p>You have to be careful when calling <TT>printf</TT>.
128 It has no way of knowing
129 how many arguments you've passed it
130 or what their types are
131 other than by looking for the format specifiers in
132 the format string.
133 If there are more format specifiers
134 (that is, more <TT>%</TT> signs)
135 than there are arguments,
136 or if the arguments have the wrong types for the format
137 specifiers,
138 <TT>printf</TT> can misbehave badly,
139 often printing nonsense numbers or (even worse) numbers which mislead
140 you into thinking that some other part of your program is broken.
141 </p><p>Because of some automatic conversion rules which we haven't
142 covered yet, you have a small amount of latitude
143 in the types of the expressions
144 you pass as arguments
145 to <TT>printf</TT>.
146 The argument for <TT>%c</TT> may be of type <TT>char</TT> or <TT>int</TT>,
147 and the argument for <TT>%d</TT> may be of type <TT>char</TT> or <TT>int</TT>.
148 The string argument for <TT>%s</TT> may be a string constant,
149 an array of characters, or a pointer to some characters
150 (though we haven't really covered strings or pointers yet).
151 Finally, the arguments corresponding to <TT>%e</TT>, <TT>%f</TT>, and <TT>%g</TT>
152 may be of types <TT>float</TT> or <TT>double</TT>.
153 But other combinations do <em>not</em> work reliably:
154 <TT>%d</TT> will not print a <TT>long int</TT> or a <TT>float</TT> or a <TT>double</TT>;
155 <TT>%ld</TT> will not print an <TT>int</TT>;
156 <TT>%e</TT>, <TT>%f</TT>, and <TT>%g</TT> will not print an <TT>int</TT>.
157 </p><hr>
159 Read sequentially:
160 <a href="sx6.html" rev=precedes>prev</a>
161 <a href="sx6b.html" rel=precedes>next</a>
162 <a href="sx6.html" rev=subdocument>up</a>
163 <a href="top.html">top</a>
164 </p>
166 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
167 // <a href="copyright.html">Copyright</a> 1995-1997
168 // <a href="mailto:scs@eskimo.com">mail feedback</a>
169 </p>
170 </body>
171 </html>