1SCANF(3) Linux Programmer's Manual SCANF(3)
2
3
4
6 scanf, fscanf, sscanf, vscanf, vsscanf, vfscanf - input format conver‐
7 sion
8
10 #include <stdio.h>
11 int scanf(const char *format, ...);
12 int fscanf(FILE *stream, const char *format, ...);
13 int sscanf(const char *str, const char *format, ...);
14
15 #include <stdarg.h>
16 int vscanf(const char *format, va_list ap);
17 int vsscanf(const char *str, const char *format, va_list ap);
18 int vfscanf(FILE *stream, const char *format, va_list ap);
19
21 The scanf() family of functions scans input according to format as
22 described below. This format may contain conversion specifications;
23 the results from such conversions, if any, are stored in the locations
24 pointed to by the pointer arguments that follow format. Each pointer
25 argument must be of a type that is appropriate for the value returned
26 by the corresponding conversion specification.
27
28 If the number of conversion specifications in format exceeds the number
29 of pointer arguments, the results are undefined. If the number of
30 pointer arguments exceeds the number of conversion specifications, then
31 the excess pointer arguments are evaluated, but are otherwise ignored.
32
33 The scanf() function reads input from the standard input stream stdin,
34 fscanf() reads input from the stream pointer stream, and sscanf() reads
35 its input from the character string pointed to by str.
36
37 The vfscanf() function is analogous to vfprintf(3) and reads input from
38 the stream pointer stream using a variable argument list of pointers
39 (see stdarg(3). The vscanf() function scans a variable argument list
40 from the standard input and the vsscanf() function scans it from a
41 string; these are analogous to the vprintf() and vsprintf() functions
42 respectively.
43
44 The format string consists of a sequence of directives which describe
45 how to process the sequence of input characters. If processing of a
46 directive fails, no further input is read, and scanf() returns. A
47 "failure" can be either of the following: input failure, meaning that
48 input characters were unavailable, or matching failure, meaning that
49 the input was inappropriate (see below).
50
51 A directive is one of the following:
52
53 · A sequence of white-space characters (space, tab, newline, etc;
54 see isspace(3)). This directive matches any amount of white
55 space, including none, in the input.
56
57 · An ordinary character (i.e., one other than white space or '%').
58 This character must exactly match the next character of input.
59
60 · A conversion specification, which commences with a '%' (percent)
61 character. A sequence of characters from the input is converted
62 according to this specification, and the result is placed in the
63 corresponding pointer argument. If the next item of input does
64 not match the the conversion specification, the conversion fails
65 — this is a matching failure.
66
67 Each conversion specification in format begins with either the charac‐
68 ter '%' or the character sequence "%n$" (see below for the distinction)
69 followed by:
70
71 · An optional '*' assignment-suppression character: scanf() reads
72 input as directed by the conversion specification, but discards
73 the input. No corresponding pointer argument is required, and
74 this specification is not included in the count of successful
75 assignments returned by scanf().
76
77 · An optional 'a' character. This is used with string conver‐
78 sions, and relieves the caller of the need to allocate a corre‐
79 sponding buffer to hold the input: instead, scanf() allocates a
80 buffer of sufficient size, and assigns the address of this buf‐
81 fer to the corresponding pointer argument, which should be a
82 pointer to a char * variable (this variable does not need to be
83 initialised before the call). The caller should subsequently
84 free(3) this buffer when it is no longer required. This is a
85 GNU extension; C99 employs the 'a' character as a conversion
86 specifier (and it can also be used as such in the GNU implemen‐
87 tation).
88
89 · An optional decimal integer which specifies the maximum field
90 width. Reading of characters stops either when this maximum is
91 reached or when a non-matching character is found, whichever
92 happens first. Most conversions discard initial whitespace
93 characters (the exceptions are noted below), and these discarded
94 characters don't count towards the maximum field width. String
95 input conversions store a null terminator ('\0') to mark the end
96 of the input; the maximum field width does not include this ter‐
97 minator.
98
99 · An optional type modifier character. For example, the l type
100 modifier is used with integer conversions such as %d to specify
101 that the corresponding pointer argument refers to a long int
102 rather than a pointer to an int.
103
104 · A conversion specifier that specifies the type of input conver‐
105 sion to be performed.
106
107 The conversion specifications in format are of two forms, either begin‐
108 ning with '%' or beginning with "%n$". The two forms should not be
109 mixed in the same format string, except that a string containing "%n$"
110 specifications can include %% and %*. If format contains '%' specifi‐
111 cations then these correspond in order with successive pointer argu‐
112 ments. In the "%n$" form (which is specified in POSIX.1-2001, but not
113 C99), n is a decimal integer that specifies that the converted input
114 should be placed in the location referred to by the n-th pointer argu‐
115 ment following format.
116
118 The following type modifier characters can appear in a conversion spec‐
119 ification:
120
121 h Indicates that the conversion will be one of diouxX or n and the
122 next pointer is a pointer to a short int or unsigned short int
123 (rather than int).
124
125 hh As for h, but the next pointer is a pointer to a signed char or
126 unsigned char.
127
128 j As for h, but the next pointer is a pointer to a intmax_t or
129 uintmax_t. This modifier was introduced in C99.
130
131 l Indicates either that the conversion will be one of diouxX or n
132 and the next pointer is a pointer to a long int or unsigned long
133 int (rather than int), or that the conversion will be one of efg
134 and the next pointer is a pointer to double (rather than float).
135 Specifying two l characters is equivalent to L. If used with %c
136 or %s the corresponding parameter is considered as a pointer to
137 a wide character or wide character string respectively.
138
139 L Indicates that the conversion will be either efg and the next
140 pointer is a pointer to long double or the conversion will be
141 dioux and the next pointer is a pointer to long long.
142
143 q equivalent to L. This specifier does not exist in ANSI C.
144
145 t As for h, but the next pointer is a pointer to a ptrdiff_t.
146 This modifier was introduced in C99.
147
148 z As for h, but the next pointer is a pointer to a size_t. This
149 modifier was introduced in C99.
150
151 The following conversion specifiers are available:
152
153 % Matches a literal '%'. That is, %% in the format string matches
154 a single input '%' character. No conversion is done, and
155 assignment does not occur.
156
157 d Matches an optionally signed decimal integer; the next pointer
158 must be a pointer to int.
159
160 D Equivalent to ld; this exists only for backwards compatibility.
161 (Note: thus only in libc4. In libc5 and glibc the %D is silently
162 ignored, causing old programs to fail mysteriously.)
163
164 i Matches an optionally signed integer; the next pointer must be a
165 pointer to int. The integer is read in base 16 if it begins
166 with 0x or 0X, in base 8 if it begins with 0, and in base 10
167 otherwise. Only characters that correspond to the base are
168 used.
169
170 o Matches an unsigned octal integer; the next pointer must be a
171 pointer to unsigned int.
172
173 u Matches an unsigned decimal integer; the next pointer must be a
174 pointer to unsigned int.
175
176 x Matches an unsigned hexadecimal integer; the next pointer must
177 be a pointer to unsigned int.
178
179 X Equivalent to x.
180
181 f Matches an optionally signed floating-point number; the next
182 pointer must be a pointer to float.
183
184 e Equivalent to f.
185
186 g Equivalent to f.
187
188 E Equivalent to f.
189
190 a (C99) Equivalent to f.
191
192 s Matches a sequence of non-white-space characters; the next
193 pointer must be a pointer to character array that is long enough
194 to hold the input sequence and the terminating null character
195 ('\0'), which is added automatically. The input string stops at
196 white space or at the maximum field width, whichever occurs
197 first.
198
199 c Matches a sequence of characters whose length is specified by
200 the maximum field width (default 1); the next pointer must be a
201 pointer to char, and there must be enough room for all the char‐
202 acters (no terminating null byte is added). The usual skip of
203 leading white space is suppressed. To skip white space first,
204 use an explicit space in the format.
205
206 [ Matches a nonempty sequence of characters from the specified set
207 of accepted characters; the next pointer must be a pointer to
208 char, and there must be enough room for all the characters in
209 the string, plus a terminating null byte. The usual skip of
210 leading white space is suppressed. The string is to be made up
211 of characters in (or not in) a particular set; the set is
212 defined by the characters between the open bracket [ character
213 and a close bracket ] character. The set excludes those charac‐
214 ters if the first character after the open bracket is a circum‐
215 flex (^). To include a close bracket in the set, make it the
216 first character after the open bracket or the circumflex; any
217 other position will end the set. The hyphen character - is also
218 special; when placed between two other characters, it adds all
219 intervening characters to the set. To include a hyphen, make it
220 the last character before the final close bracket. For
221 instance, [^]0-9-] means the set "everything except close
222 bracket, zero through nine, and hyphen". The string ends with
223 the appearance of a character not in the (or, with a circumflex,
224 in) set or when the field width runs out.
225
226 p Matches a pointer value (as printed by %p in printf(3); the next
227 pointer must be a pointer to a pointer to void.
228
229 n Nothing is expected; instead, the number of characters consumed
230 thus far from the input is stored through the next pointer,
231 which must be a pointer to int. This is not a conversion,
232 although it can be suppressed with the * assignment-suppression
233 character. The C standard says: "Execution of a %n directive
234 does not increment the assignment count returned at the comple‐
235 tion of execution" but the Corrigendum seems to contradict this.
236 Probably it is wise not to make any assumptions on the effect of
237 %n conversions on the return value.
238
240 These functions return the number of input items successfully matched
241 and assigned, which can be fewer than provided for, or even zero in the
242 event of an early matching failure.
243
244 The value EOF is returned if the end of input is reached before either
245 the first successful conversion or a matching failure occurs. EOF is
246 also returned if a read error occurs, in which case the error indicator
247 for the stream (see ferror(3)) is set, and errno is set indicate the
248 error.
249
251 getc(3), printf(3), setlocale(3), strtod(3), strtol(3), strtoul(3)
252
254 The functions fscanf(), scanf(), and sscanf() conform to C89 and C99.
255
256 The q specifier is the 4.4BSD notation for long long, while ll or the
257 usage of L in integer conversions is the GNU notation.
258
259 The Linux version of these functions is based on the GNU libio library.
260 Take a look at the info documentation of GNU libc (glibc-1.08) for a
261 more concise description.
262
264 All functions are fully C89 conformant, but provide the additional
265 specifiers q and a as well as an additional behaviour of the L and l
266 specifiers. The latter may be considered to be a bug, as it changes the
267 behaviour of specifiers defined in C89.
268
269 Some combinations of the type modifiers and conversion specifiers
270 defined by ANSI C do not make sense (e.g. %Ld). While they may have a
271 well-defined behaviour on Linux, this need not to be so on other archi‐
272 tectures. Therefore it usually is better to use modifiers that are not
273 defined by ANSI C at all, i.e. use q instead of L in combination with
274 diouxX conversions or ll.
275
276 The usage of q is not the same as on 4.4BSD, as it may be used in float
277 conversions equivalently to L.
278
279
280
281LINUX MANPAGE 1995-11-01 SCANF(3)