1 .\" Copyright (c) 1990, 1991, 1993
2 .\" The Regents of the University of California. All rights reserved.
4 .\" This code is derived from software contributed to Berkeley by
5 .\" Chris Torek and the American National Standards Committee X3,
6 .\" on Information Processing Systems.
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\" notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\" notice, this list of conditions and the following disclaimer in the
15 .\" documentation and/or other materials provided with the distribution.
16 .\" 3. All advertising materials mentioning features or use of this software
17 .\" must display the following acknowledgement:
18 .\" This product includes software developed by the University of
19 .\" California, Berkeley and its contributors.
20 .\" 4. Neither the name of the University nor the names of its contributors
21 .\" may be used to endorse or promote products derived from this software
22 .\" without specific prior written permission.
24 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 .\" @(#)scanf.3 8.2 (Berkeley) 12/11/93
37 .\" $FreeBSD: src/lib/libc/stdio/scanf.3,v 1.24 2003/06/28 09:03:25 das Exp $
38 .\" $DragonFly: src/lib/libc/stdio/scanf.3,v 1.3 2006/08/26 10:27:55 swildner Exp $
50 .Nd input format conversion
56 .Fn scanf "const char *format" ...
58 .Fn fscanf "FILE *stream" "const char *format" ...
60 .Fn sscanf "const char *str" "const char *format" ...
63 .Fn vscanf "const char *format" "va_list ap"
65 .Fn vsscanf "const char *str" "const char *format" "va_list ap"
67 .Fn vfscanf "FILE *stream" "const char *format" "va_list ap"
71 family of functions scans input according to a
74 This format may contain
75 .Em conversion specifiers ;
76 the results from such conversions, if any,
77 are stored through the
83 reads input from the standard input stream
86 reads input from the stream pointer
90 reads its input from the character string pointed to by
97 and reads input from the stream pointer
99 using a variable argument list of pointers (see
103 function scans a variable argument list from the standard input and
106 function scans it from a string;
107 these are analogous to
112 functions respectively.
115 argument must correspond properly with
116 each successive conversion specifier
120 All conversions are introduced by the
122 (percent sign) character.
126 may also contain other characters.
127 White space (such as blanks, tabs, or newlines) in the
129 string match any amount of white space, including none, in the input.
133 when an input character does not match such a format character.
135 when an input conversion cannot be made (see below).
139 character introducing a conversion
140 there may be a number of
142 characters, as follows:
143 .Bl -tag -width ".Cm l No (ell)"
145 Suppresses assignment.
146 The conversion that follows occurs as usual, but no pointer is used;
147 the result of the conversion is simply discarded.
149 Indicates that the conversion will be one of
153 and the next pointer is a pointer to a
158 Indicates that the conversion will be one of
162 and the next pointer is a pointer to a
167 Indicates that the conversion will be one of
171 and the next pointer is a pointer to a
175 that the conversion will be one of
179 and the next pointer is a pointer to
183 or that the conversion will be one of
188 and the next pointer is a pointer to an array of
192 .It Cm ll No (ell ell)
193 Indicates that the conversion will be one of
197 and the next pointer is a pointer to a
202 Indicates that the conversion will be one of
206 and the next pointer is a pointer to
209 Indicates that the conversion will be one of
213 and the next pointer is a pointer to a
218 Indicates that the conversion will be one of
222 and the next pointer is a pointer to a
227 Indicates that the conversion will be one of
231 and the next pointer is a pointer to a
237 Indicates that the conversion will be one of
241 and the next pointer is a pointer to a
247 In addition to these flags,
248 there may be an optional maximum field width,
249 expressed as a decimal integer,
253 If no width is given,
256 is used (with one exception, below);
257 otherwise at most this many bytes are scanned
258 in processing the conversion.
264 conversions, the field width specifies the maximum number
265 of multibyte characters that will be scanned.
266 Before conversion begins,
267 most conversions skip white space;
268 this white space is not counted against the field width.
270 The following conversions are available:
278 matches a single input
281 No conversion is done, and assignment does not occur.
283 Matches an optionally signed decimal integer;
284 the next pointer must be a pointer to
287 Matches an optionally signed integer;
288 the next pointer must be a pointer to
290 The integer is read in base 16 if it begins
295 in base 8 if it begins with
297 and in base 10 otherwise.
298 Only characters that correspond to the base are used.
300 Matches an octal integer;
301 the next pointer must be a pointer to
304 Matches an optionally signed decimal integer;
305 the next pointer must be a pointer to
308 Matches an optionally signed hexadecimal integer;
309 the next pointer must be a pointer to
311 .It Cm e , E , f , F , g , G
312 Matches a floating-point number in the style of
314 The next pointer must be a pointer to
322 Matches a sequence of non-white-space characters;
323 the next pointer must be a pointer to
325 and the array must be large enough to accept all the sequence and the
329 The input string stops at white space
330 or at the maximum field width, whichever occurs first.
334 qualifier is present, the next pointer must be a pointer to
336 into which the input will be placed after conversion by
342 Matches a sequence of
345 characters (default 1);
346 the next pointer must be a pointer to
348 and there must be enough room for all the characters
352 The usual skip of leading white space is suppressed.
353 To skip white space first, use an explicit space in the format.
357 qualifier is present, the next pointer must be a pointer to
359 into which the input will be placed after conversion by
365 Matches a nonempty sequence of characters from the specified set
366 of accepted characters;
367 the next pointer must be a pointer to
369 and there must be enough room for all the characters in the string,
373 The usual skip of leading white space is suppressed.
374 The string is to be made up of characters in
377 the set is defined by the characters between the open bracket
386 if the first character after the open bracket is a circumflex
388 To include a close bracket in the set,
389 make it the first character after the open bracket
391 any other position will end the set.
395 when placed between two other characters,
396 it adds all intervening characters to the set.
398 make it the last character before the final close bracket.
402 .Dq "everything except close bracket, zero through nine, and hyphen" .
403 The string ends with the appearance of a character not in the
404 (or, with a circumflex, in) set
405 or when the field width runs out.
409 qualifier is present, the next pointer must be a pointer to
411 into which the input will be placed after conversion by
414 Matches a pointer value (as printed by
418 the next pointer must be a pointer to
422 instead, the number of characters consumed thus far from the input
423 is stored through the next pointer,
424 which must be a pointer to
428 a conversion, although it can be suppressed with the
434 character is defined in the program's locale (category
437 For backwards compatibility, a
441 causes an immediate return of
447 the number of input items assigned, which can be fewer than provided
448 for, or even zero, in the event of a matching failure.
450 indicates that, while there was input available,
451 no conversions were assigned;
452 typically this is due to an invalid input character,
453 such as an alphabetic character for a
458 is returned if an input failure occurs before any conversion such as an
460 If an error or end-of-file occurs after conversion
462 the number of conversions which were successfully completed is returned.
483 Earlier implementations of
486 .Cm \&%D , \&%E , \&%F , \&%O
489 as their lowercase equivalents with an
494 treated an unknown conversion character as
498 depending on its case.
499 This functionality has been removed.
501 Numerical strings are truncated to 512 characters; for example,
512 modifiers for positional arguments are not implemented.
516 family of functions do not correctly handle multibyte characters in the