-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintf.html
More file actions
264 lines (205 loc) · 7.75 KB
/
printf.html
File metadata and controls
264 lines (205 loc) · 7.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<!-- Mirrored from cppreference.com/stdio/printf.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:23:44 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
<head>
<meta name="generator" content=
"HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">
<title>printf</title>
<link href="../cppreference.css" rel="stylesheet" type="text/css">
<link href="../prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../prettify.js"></script>
</head>
<body onload="prettyPrint()">
<table>
<tr>
<td>
<div class="body-content">
<div class="header-box">
<a href="../index-2.html">cppreference.com</a> > <a href=
"index.html">Standard C I/O</a> > <a href=
"printf.html">printf</a>
</div>
<div class="name-format">
printf
</div>
<div class="syntax-name-format">
Syntax:
</div>
<pre class="syntax-box">
#include <cstdio>
int printf( const char *format, ... );
</pre>
<p>The printf() function prints output to <strong>stdout</strong>,
according to <em>format</em> and other arguments passed to printf().
The string <em>format</em> consists of two types of items -
characters that will be printed to the screen, and format commands
that define how the other arguments to printf() are displayed.
Basically, you specify a format string that has text in it, as well
as "special" characters that map to the other arguments of
printf(). For example, this code</p>
<pre class="prettyprint">
char name[20] = "Bob";
int age = 21;
printf( "Hello %s, you are %d years old\n", name, age );
</pre>
<p>displays the following output:</p>
<pre class="prettyprint">
Hello Bob, you are 21 years old
</pre>
<p>The %s means, "insert the first argument, a string, right
here." The %d indicates that the second argument (an integer)
should be placed there. There are different %-codes for different
variable types, as well as options to limit the length of the
variables and whatnot.</p>
<table class="code-table">
<tr>
<th class="code-table-th">Code</th>
<th class="code-table-th">Format</th>
</tr>
<tr>
<td class="code-table-td">%c</td>
<td class="code-table-td">character</td>
</tr>
<tr>
<td class="code-table-td">%d</td>
<td class="code-table-td">signed integers</td>
</tr>
<tr>
<td class="code-table-td">%i</td>
<td class="code-table-td">signed integers</td>
</tr>
<tr>
<td class="code-table-td">%e</td>
<td class="code-table-td">scientific notation, with a lowercase
"e"</td>
</tr>
<tr>
<td class="code-table-td">%E</td>
<td class="code-table-td">scientific notation, with a uppercase
"E"</td>
</tr>
<tr>
<td class="code-table-td">%f</td>
<td class="code-table-td">floating point</td>
</tr>
<tr>
<td class="code-table-td">%g</td>
<td class="code-table-td">use %e or %f, whichever is shorter</td>
</tr>
<tr>
<td class="code-table-td">%G</td>
<td class="code-table-td">use %E or %f, whichever is shorter</td>
</tr>
<tr>
<td class="code-table-td">%o</td>
<td class="code-table-td">octal</td>
</tr>
<tr>
<td class="code-table-td">%s</td>
<td class="code-table-td">a string of characters</td>
</tr>
<tr>
<td class="code-table-td">%u</td>
<td class="code-table-td">unsigned integer</td>
</tr>
<tr>
<td class="code-table-td">%x</td>
<td class="code-table-td">unsigned hexadecimal, with lowercase
letters</td>
</tr>
<tr>
<td class="code-table-td">%X</td>
<td class="code-table-td">unsigned hexadecimal, with uppercase
letters</td>
</tr>
<tr>
<td class="code-table-td">%p</td>
<td class="code-table-td">a pointer</td>
</tr>
<tr>
<td class="code-table-td">%n</td>
<td class="code-table-td">the argument shall be a pointer to an
integer into which is placed the number of characters written so
far</td>
</tr>
<tr>
<td class="code-table-td">%%</td>
<td class="code-table-td">a '%' sign</td>
</tr>
</table>
<p>An integer placed between a % sign and the format command acts as
a minimum field width specifier, and pads the output with spaces or
zeros to make it long enough. If you want to pad with zeros, place a
zero before the minimum field width specifier:</p>
<pre class="prettyprint">
%012d
</pre>
<p>You can also include a precision modifier, in the form of a .N
where N is some number, before the format command:</p>
<pre class="prettyprint">
%012.4d
</pre>
<p>The precision modifier has different meanings depending on the
format command being used:</p>
<ul>
<li>With %e, %E, and %f, the precision modifier lets you specify
the number of decimal places desired. For example, %12.6f will
display a floating number at least 12 digits wide, with six decimal
places.</li>
<li>With %g and %G, the precision modifier determines the maximum
number of significant digits displayed.</li>
<li>With %s, the precision modifer simply acts as a maximumfield
length, to complement the minimum field length that precedes the
period.</li>
</ul>
<p>All of printf()'s output is right-justified, unless you place
a minus sign right after the % sign. For example,</p>
<pre class="prettyprint">
%-12.4f
</pre>
<p>will display a floating point number with a minimum of 12
characters, 4 decimal places, and left justified. You may modify the
%d, %i, %o, %u, and %x type specifiers with the letter l and the
letter h to specify long and short <a href="../data_types.html">data
types</a> (e.g. %hd means a short integer). The %e, %f, and %g type
specifiers can have the letter l before them to indicate that a
double follows. The %g, %f, and %e type specifiers can be preceded
with the character '#' to ensure that the decimal point will
be present, even if there are no decimal digits. The use of the
'#' character with the %x type specifier indicates that the
hexidecimal number should be printed with the '0x' prefix.
The use of the '#' character with the %o type specifier
indicates that the octal value should be displayed with a 0
prefix.</p>
<p>Inserting a plus sign '+' into the type specifier will force
positive values to be preceded by a '+' sign. Putting a space
character ' ' there will force positive values to be preceded by a
single space character.</p>
<p>You can also include <a href="../escape_sequences.html">constant
escape sequences</a> in the output string.</p>
<p>The return value of printf() is the number of characters printed,
or a negative number if an error occurred.</p>
<div class="related-name-format">
Related topics:
</div>
<div class="related-content">
<a href="fprintf.html">fprintf</a><br>
<a href="puts.html">puts</a><br>
<a href="scanf.html">scanf</a><br>
<a href="sprintf.html">sprintf</a>
</div>
</div>
</td>
<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
</tr>
</table>
</body>
<!-- Mirrored from cppreference.com/stdio/printf.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:23:44 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
</html>