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 .\" 4. Neither the name of the University nor the names of its contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" @(#)scanf.3 8.2 (Berkeley) 12/11/93
33 .\" $FreeBSD: src/lib/libc/stdio/scanf.3,v 1.25 2007/01/09 00:28:07 imp Exp $
45 .Nd input format conversion
52 .Fa "FILE *restrict stream"
53 .Fa "const char *restrict format" ...
57 .Fa "const char *restrict format" ...
61 .Fa "const char *restrict s"
62 .Fa "const char *restrict format" ...
68 .Fa "FILE *restrict stream"
69 .Fa "const char *restrict format"
74 .Fa "const char *restrict format"
79 .Fa "const char *restrict s"
80 .Fa "const char *restrict format"
86 family of functions scans input according to a
89 This format may contain
90 .Em conversion specifiers ;
91 the results from such conversions, if any,
92 are stored through the
98 reads input from the standard input stream
101 reads input from the stream pointer
105 reads its input from the character string pointed to by
113 and reads input from the stream pointer
115 using a variable argument list of pointers (see
119 function scans a variable argument list from the standard input and
122 function scans it from a string;
123 these are analogous to
128 functions, respectively.
132 argument must correspond properly with
133 each successive conversion specifier
137 All conversions are introduced by the
139 (percent sign) character.
143 may also contain other characters.
144 White space (such as blanks, tabs, or newlines) in the
146 string match any amount of white space, including none, in the input.
150 when an input character does not match such a format character.
152 when an input conversion cannot be made (see below).
154 Extended locale versions of these functions are documented in
158 for more information.
162 character introducing a conversion,
163 there may be a number of
165 characters, as follows:
166 .Bl -tag -width ".Cm l No (ell)"
168 Suppresses assignment.
169 The conversion that follows occurs as usual, but no pointer is used;
170 the result of the conversion is simply discarded.
172 Indicates that the conversion will be one of
176 and the next pointer is a pointer to a
181 Indicates that the conversion will be one of
185 and the next pointer is a pointer to a
190 Indicates that the conversion will be one of
194 and the next pointer is a pointer to a
198 that the conversion will be one of
202 and the next pointer is a pointer to
206 or that the conversion will be one of
211 and the next pointer is a pointer to an array of
215 .It Cm ll No (ell ell)
216 Indicates that the conversion will be one of
220 and the next pointer is a pointer to a
225 Indicates that the conversion will be one of
229 and the next pointer is a pointer to
232 Indicates that the conversion will be one of
236 and the next pointer is a pointer to a
241 Indicates that the conversion will be one of
245 and the next pointer is a pointer to a
250 Indicates that the conversion will be one of
254 and the next pointer is a pointer to a
260 Indicates that the conversion will be one of
264 and the next pointer is a pointer to a
270 In addition to these flags,
271 there may be an optional maximum field width,
272 expressed as a decimal integer,
276 If no width is given,
279 is used (with one exception, below);
280 otherwise at most this many bytes are scanned
281 in processing the conversion.
287 conversions, the field width specifies the maximum number
288 of multibyte characters that will be scanned.
289 Before conversion begins,
290 most conversions skip white space;
291 this white space is not counted against the field width.
293 The following conversions are available:
301 matches a single input
304 No conversion is done, and assignment does not occur.
306 Matches an optionally signed decimal integer;
307 the next pointer must be a pointer to
310 Matches an optionally signed integer;
311 the next pointer must be a pointer to
313 The integer is read in base 16 if it begins
318 in base 8 if it begins with
320 and in base 10 otherwise.
321 Only characters that correspond to the base are used.
323 Matches an octal integer;
324 the next pointer must be a pointer to
327 Matches an optionally signed decimal integer;
328 the next pointer must be a pointer to
331 Matches an optionally signed hexadecimal integer;
332 the next pointer must be a pointer to
334 .It Cm a , A , e , E , f , F , g , G
335 Matches a floating-point number in the style of
337 The next pointer must be a pointer to
345 Matches a sequence of non-white-space characters;
346 the next pointer must be a pointer to
348 and the array must be large enough to accept all the sequence and the
352 The input string stops at white space
353 or at the maximum field width, whichever occurs first.
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 sequence of
368 characters (default 1);
369 the next pointer must be a pointer to
371 and there must be enough room for all the characters
375 The usual skip of leading white space is suppressed.
376 To skip white space first, use an explicit space in the format.
380 qualifier is present, the next pointer must be a pointer to
382 into which the input will be placed after conversion by
388 Matches a nonempty sequence of characters from the specified set
389 of accepted characters;
390 the next pointer must be a pointer to
392 and there must be enough room for all the characters in the string,
396 The usual skip of leading white space is suppressed.
397 The string is to be made up of characters in
400 the set is defined by the characters between the open bracket
409 if the first character after the open bracket is a circumflex
411 To include a close bracket in the set,
412 make it the first character after the open bracket
414 any other position will end the set.
418 when placed between two other characters,
419 it adds all intervening characters to the set.
421 make it the last character before the final close bracket.
425 .Dq "everything except close bracket, zero through nine, and hyphen" .
426 The string ends with the appearance of a character not in the
427 (or, with a circumflex, in) set
428 or when the field width runs out.
432 qualifier is present, the next pointer must be a pointer to
434 into which the input will be placed after conversion by
437 Matches a pointer value (as printed by
441 the next pointer must be a pointer to
443 (or other pointer type).
446 instead, the number of characters consumed thus far from the input
447 is stored through the next pointer,
448 which must be a pointer to
452 a conversion, although it can be suppressed with the
458 character is defined in the program's locale (category
461 For backwards compatibility, a
465 causes an immediate return of
468 These functions return the number of input items assigned.
469 This can be fewer than provided for, or even zero,
470 in the event of a matching failure.
471 Zero indicates that, although there was input available,
472 no conversions were assigned;
473 typically this is due to an invalid input character,
474 such as an alphabetic character for a
479 is returned if an input failure occurs before any conversion such as an
481 If an error or end-of-file occurs after conversion
483 the number of conversions which were successfully completed is returned.
505 Earlier implementations of
508 .Cm \&%D , \&%E , \&%F , \&%O
511 as their lowercase equivalents with an
516 treated an unknown conversion character as
520 depending on its case.
521 This functionality has been removed.
523 Numerical strings are truncated to 512 characters; for example,
534 modifiers for positional arguments are not implemented.
538 family of functions do not correctly handle multibyte characters in the