1 .\" $NetBSD: scanf.3,v 1.22 2009/12/14 04:40:29 dholland Exp $
3 .\" Copyright (c) 1990, 1991, 1993
4 .\" The Regents of the University of California. All rights reserved.
6 .\" This code is derived from software contributed to Berkeley by
7 .\" Chris Torek and the American National Standards Committee X3,
8 .\" on Information Processing Systems.
10 .\" Redistribution and use in source and binary forms, with or without
11 .\" modification, are permitted provided that the following conditions
13 .\" 1. Redistributions of source code must retain the above copyright
14 .\" notice, this list of conditions and the following disclaimer.
15 .\" 2. Redistributions in binary form must reproduce the above copyright
16 .\" notice, this list of conditions and the following disclaimer in the
17 .\" documentation and/or other materials provided with the distribution.
18 .\" 3. Neither the name of the University nor the names of its contributors
19 .\" may be used to endorse or promote products derived from this software
20 .\" without specific prior written permission.
22 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 .\" @(#)scanf.3 8.2 (Berkeley) 12/11/93
46 .Nd input format conversion
52 .Fn scanf "const char * restrict format" ...
54 .Fn fscanf "FILE * restrict stream" "const char * restrict format" ...
56 .Fn sscanf "const char * restrict str" "const char * restrict format" ...
59 .Fn vscanf "const char * restrict format" "va_list ap"
61 .Fn vsscanf "const char * restrict str" "const char * restrict format" "va_list ap"
63 .Fn vfscanf "FILE * restrict stream" "const char * restrict format" "va_list ap"
67 family of functions scans input according to a
70 This format may contain
71 .Em conversion specifiers ;
72 the results from such conversions, if any,
73 are stored through the
79 reads input from the standard input stream
82 reads input from the stream pointer
86 reads its input from the character string pointed to by
93 and reads input from the stream pointer
95 using a variable argument list of pointers (see
99 function scans a variable argument list from the standard input and
102 function scans it from a string;
103 these are analogous to
108 functions respectively.
111 argument must correspond properly with
112 each successive conversion specifier
113 (but see `suppression' below).
114 All conversions are introduced by the
116 (percent sign) character.
120 may also contain other characters.
121 White space (such as blanks, tabs, or newlines) in the
123 string match any amount of white space, including none, in the input.
127 when an input character does not match such a format character.
129 when an input conversion cannot be made (see below).
133 character introducing a conversion
134 there may be a number of
136 characters, as follows:
137 .Bl -tag -width indent
139 Suppresses assignment.
140 The conversion that follows occurs as usual, but no pointer is used;
141 the result of the conversion is simply discarded.
143 Indicates that the conversion will be one of
147 and the next pointer is a pointer to a
152 Indicates that the conversion will be one of
156 and the next pointer is a pointer to a
161 Indicates that the conversion will be one of
165 and the next pointer is a pointer to an
170 Indicates either that the conversion will be one of
174 and the next pointer is a pointer to a
178 or that the conversion will be one of
180 and the next pointer is a pointer to
185 Indicates that the conversion will be one of
189 and the next pointer is a pointer to a
194 Indicates that the conversion will be one of
198 and the next pointer is a pointer to a
203 Indicates that the conversion will be one of
207 and the next pointer is a pointer to a
212 Indicates that the conversion will be one of
216 and the next pointer is a pointer to a
221 Indicates that the conversion will be
223 and the next pointer is a pointer to
227 In addition to these flags,
228 there may be an optional maximum field width,
229 expressed as a decimal integer,
233 If no width is given,
234 a default of `infinity' is used (with one exception, below);
235 otherwise at most this many characters are scanned
236 in processing the conversion.
237 Before conversion begins,
238 most conversions skip white space;
239 this white space is not counted against the field width.
241 The following conversions are available:
244 Matches a literal `%'.
245 That is, `%\&%' in the format string
246 matches a single input `%' character.
247 No conversion is done, and assignment does not occur.
249 Matches an optionally signed decimal integer;
250 the next pointer must be a pointer to
255 this exists only for backwards compatibility.
257 Matches an optionally signed integer;
258 the next pointer must be a pointer to
260 The integer is read in base 16 if it begins
265 in base 8 if it begins with
267 and in base 10 otherwise.
268 Only characters that correspond to the base are used.
270 Matches an octal integer;
271 the next pointer must be a pointer to
276 this exists for backwards compatibility.
278 Matches an optionally signed decimal integer;
279 the next pointer must be a pointer to
282 Matches an optionally signed hexadecimal integer;
283 the next pointer must be a pointer to
289 Matches an optionally signed floating-point number;
290 the next pointer must be a pointer to
305 Matches a sequence of non-white-space characters;
306 the next pointer must be a pointer to
308 and the array must be large enough to accept all the sequence and the
312 The input string stops at white space
313 or at the maximum field width, whichever occurs first.
315 Matches a sequence of
318 characters (default 1);
319 the next pointer must be a pointer to
321 and there must be enough room for all the characters
325 The usual skip of leading white space is suppressed.
326 To skip white space first, use an explicit space in the format.
328 Matches a nonempty sequence of characters from the specified set
329 of accepted characters;
330 the next pointer must be a pointer to
332 and there must be enough room for all the characters in the string,
336 The usual skip of leading white space is suppressed.
337 The string is to be made up of characters in
340 the set is defined by the characters between the open bracket
349 if the first character after the open bracket is a circumflex
351 To include a close bracket in the set,
352 make it the first character after the open bracket
354 any other position will end the set.
358 when placed between two other characters,
359 it adds all intervening characters to the set.
361 make it the last character before the final close bracket.
364 means the set `everything except close bracket, zero through nine,
366 The string ends with the appearance of a character not in the
367 (or, with a circumflex, in) set
368 or when the field width runs out.
370 Matches a pointer value (as printed by
374 the next pointer must be a pointer to
378 instead, the number of characters consumed thus far from the input
379 is stored through the next pointer,
380 which must be a pointer to
384 a conversion, although it can be suppressed with the
389 For backwards compatibility,
390 other conversion characters (except
392 are taken as if they were
396 and a `conversion' of
398 causes an immediate return of
404 the number of input items assigned, which can be fewer than provided
405 for, or even zero, in the event of a matching failure.
407 indicates that, while there was input available,
408 no conversions were assigned;
409 typically this is due to an invalid input character,
410 such as an alphabetic character for a
415 is returned if an input failure occurs before any conversion such as an
417 If an error or end-of-file occurs after conversion has begun,
418 the number of conversions which were successfully completed is returned.
438 conversion format modifiers
459 All of the backwards compatibility formats will be removed in the future.
461 Numerical strings are truncated to 512 characters; for example,