1 .\" $NetBSD: scanf.3,v 1.27 2010/05/14 03:04:32 joerg 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
80 reads input from the standard input stream
83 reads input from the stream pointer
87 reads its input from the character string pointed to by
94 and reads input from the stream pointer
96 using a variable argument list of pointers (see
100 function scans a variable argument list from the standard input and
103 function scans it from a string;
104 these are analogous to
109 functions respectively.
113 argument must correspond properly with
114 each successive conversion specifier
115 (but see `suppression' below).
116 All conversions are introduced by the
118 (percent sign) character.
122 may also contain other characters.
123 White space (such as blanks, tabs, or newlines) in the
125 string match any amount of white space, including none, in the input.
129 when an input character does not match such a format character.
131 when an input conversion cannot be made (see below).
135 character introducing a conversion
136 there may be a number of
138 characters, as follows:
139 .Bl -tag -width indent
141 Suppresses assignment.
142 The conversion that follows occurs as usual, but no pointer is used;
143 the result of the conversion is simply discarded.
145 Indicates that the conversion will be one of
149 and the next pointer is a pointer to a
154 Indicates that the conversion will be one of
158 and the next pointer is a pointer to a
163 Indicates that the conversion will be one of
167 and the next pointer is a pointer to an
172 Indicates either that the conversion will be one of
176 and the next pointer is a pointer to a
180 or that the conversion will be one of
182 and the next pointer is a pointer to
187 Indicates that the conversion will be one of
191 and the next pointer is a pointer to a
196 Indicates that the conversion will be one of
200 and the next pointer is a pointer to a
205 Indicates that the conversion will be one of
209 and the next pointer is a pointer to a
214 Indicates that the conversion will be one of
218 and the next pointer is a pointer to a
223 Indicates that the conversion will be
225 and the next pointer is a pointer to
229 In addition to these flags,
230 there may be an optional maximum field width,
231 expressed as a decimal integer,
235 If no width is given,
236 a default of `infinity' is used (with one exception, below);
237 otherwise at most this many characters are scanned
238 in processing the conversion.
239 Before conversion begins,
240 most conversions skip white space;
241 this white space is not counted against the field width.
243 The following conversions are available:
246 Matches a literal `%'.
247 That is, `%\&%' in the format string
248 matches a single input `%' character.
249 No conversion is done, and assignment does not occur.
251 Matches an optionally signed decimal integer;
252 the next pointer must be a pointer to
257 this exists only for backwards compatibility.
259 Matches an optionally signed integer;
260 the next pointer must be a pointer to
262 The integer is read in base 16 if it begins
267 in base 8 if it begins with
269 and in base 10 otherwise.
270 Only characters that correspond to the base are used.
272 Matches an octal integer;
273 the next pointer must be a pointer to
278 this exists for backwards compatibility.
280 Matches an optionally signed decimal integer;
281 the next pointer must be a pointer to
284 Matches an optionally signed hexadecimal integer;
285 the next pointer must be a pointer to
291 Matches an optionally signed floating-point number;
292 the next pointer must be a pointer to
307 Matches a sequence of non-white-space characters;
308 the next pointer must be a pointer to
310 and the array must be large enough to accept all the sequence and the
314 The input string stops at white space
315 or at the maximum field width, whichever occurs first.
317 Matches a sequence of
320 characters (default 1);
321 the next pointer must be a pointer to
323 and there must be enough room for all the characters
327 The usual skip of leading white space is suppressed.
328 To skip white space first, use an explicit space in the format.
330 Matches a nonempty sequence of characters from the specified set
331 of accepted characters;
332 the next pointer must be a pointer to
334 and there must be enough room for all the characters in the string,
338 The usual skip of leading white space is suppressed.
339 The string is to be made up of characters in
342 the set is defined by the characters between the open bracket
351 if the first character after the open bracket is a circumflex
353 To include a close bracket in the set,
354 make it the first character after the open bracket
356 any other position will end the set.
360 when placed between two other characters,
361 it adds all intervening characters to the set.
363 make it the last character before the final close bracket.
366 means the set `everything except close bracket, zero through nine,
368 The string ends with the appearance of a character not in the
369 (or, with a circumflex, in) set
370 or when the field width runs out.
372 Matches a pointer value (as printed by
376 the next pointer must be a pointer to
380 instead, the number of characters consumed thus far from the input
381 is stored through the next pointer,
382 which must be a pointer to
386 a conversion, although it can be suppressed with the
391 For backwards compatibility,
392 other conversion characters (except
394 are taken as if they were
398 and a `conversion' of
400 causes an immediate return of
403 The format string specifier macros described in
405 should be used for the standard
407 fixed-size integers documented in
413 the number of input items assigned, which can be fewer than provided
414 for, or even zero, in the event of a matching failure.
416 indicates that, while there was input available,
417 no conversions were assigned;
418 typically this is due to an invalid input character,
419 such as an alphabetic character for a
424 is returned if an input failure occurs before any conversion such as an
426 If an error or end-of-file occurs after conversion has begun,
427 the number of conversions which were successfully completed is returned.
448 conversion format modifiers
469 All of the backwards compatibility formats will be removed in the future.
471 Numerical strings are truncated to 512 characters; for example,