-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanf.html
More file actions
200 lines (151 loc) · 5.32 KB
/
scanf.html
File metadata and controls
200 lines (151 loc) · 5.32 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
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<!-- Mirrored from cppreference.com/stdio/scanf.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:25:42 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>scanf</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="scanf.html">scanf</a>
</div>
<div class="name-format">
scanf
</div>
<div class="syntax-name-format">
Syntax:
</div>
<pre class="syntax-box">
#include <cstdio>
int scanf( const char *format, ... );
</pre>
<p>The scanf() function reads input from <strong>stdin</strong>,
according to the given <em>format</em>, and stores the data in the
other arguments. It works a lot like <a href=
"printf.html">printf</a>(). The <em>format</em> string consists of
control characters, whitespace characters, and non-whitespace
characters. The control characters are preceded by a % sign, and are
as follows:</p>
<table class="code-table">
<tr>
<th class="code-table-th">Control Character</th>
<th class="code-table-th">Explanation</th>
</tr>
<tr>
<td class="code-table-td">%c</td>
<td class="code-table-td">a single character</td>
</tr>
<tr>
<td class="code-table-td">%d</td>
<td class="code-table-td">a decimal integer</td>
</tr>
<tr>
<td class="code-table-td">%i</td>
<td class="code-table-td">an integer</td>
</tr>
<tr>
<td class="code-table-td">%e, %f, %g</td>
<td class="code-table-td">a floating-point number</td>
</tr>
<tr>
<td class="code-table-td">%lf</td>
<td class="code-table-td">a double</td>
</tr>
<tr>
<td class="code-table-td">%o</td>
<td class="code-table-td">an octal number</td>
</tr>
<tr>
<td class="code-table-td">%s</td>
<td class="code-table-td">a string</td>
</tr>
<tr>
<td class="code-table-td">%x</td>
<td class="code-table-td">a hexadecimal number</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">an integer equal to the number of
characters read so far</td>
</tr>
<tr>
<td class="code-table-td">%u</td>
<td class="code-table-td">an unsigned integer</td>
</tr>
<tr>
<td class="code-table-td">%[]</td>
<td class="code-table-td">a set of characters</td>
</tr>
<tr>
<td class="code-table-td">%%</td>
<td class="code-table-td">a percent sign</td>
</tr>
</table>
<p>scanf() reads the input, matching the characters from format. When
a control character is read, it puts the value in the next variable.
Whitespace (tabs, spaces, etc) are skipped. Non-whitespace characters
are matched to the input, then discarded. If a number comes between
the % sign and the control character, then only that many characters
will be converted into the variable. If scanf() encounters a set of
characters, denoted by the %[] control character, then any characters
found within the brackets are read into the variable. The return
value of scanf() is the number of variables that were successfully
assigned values, or <strong>EOF</strong> if there is an error.</p>
<div class="related-examples-format">
Example code:
</div>
<div class="related-examples">
<p>This code snippet uses scanf() to read an int, float, and a
double from the user. Note that the variable arguments to scanf()
are passed in by address, as denoted by the ampersand (&)
preceding each variable:</p>
<pre class="prettyprint">
int i;
float f;
double d;
printf( "Enter an integer: " );
scanf( "%d", &i );
printf( "Enter a float: " );
scanf( "%f", &f );
printf( "Enter a double: " );
scanf( "%lf", &d );
printf( "You entered %d, %f, and %f\n", i, f, d );
</pre>
</div>
<div class="related-name-format">
Related topics:
</div>
<div class="related-content">
<a href="fgets.html">fgets</a><br>
<a href="fscanf.html">fscanf</a><br>
<a href="printf.html">printf</a><br>
<a href="sscanf.html">sscanf</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/scanf.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:25:42 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
</html>