1 .\" Copyright (c) 1990, 1991 The Regents of the University of California.
2 .\" 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 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\" notice, this list of conditions and the following disclaimer in the
16 .\" documentation and/or other materials provided with the distribution.
17 .\" 3. All advertising materials mentioning features or use of this software
18 .\" must display the following acknowledgement:
19 .\" This product includes software developed by the University of
20 .\" California, Berkeley and its contributors.
21 .\" 4. Neither the name of the University nor the names of its contributors
22 .\" may be used to endorse or promote products derived from this software
23 .\" without specific prior written permission.
25 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 .\" @(#)scanf.3 6.14 (Berkeley) 1/8/93
40 .\" Converted for Linux, Mon Nov 29 15:22:01 1993, faith@cs.unc.edu
41 .\" modified to resemble the GNU libio setup used in the Linux libc
42 .\" used in versions 4.x (x>4) and 5 Helmut.Geyer@iwr.uni-heidelberg.de
43 .\" Modified, aeb, 970121
44 .\" 2005-07-14, mtk, added description of %n$ form; various text
45 .\" incorporated from the GNU C library documentation ((C) The
46 .\" Free Software Foundation); other parts substantially rewritten.
49 .\" Add ERRORS section.
50 .\" Document the 'a' and 'm' modifiers for dynamic string allocation.
52 .TH SCANF 3 2017-09-15 "GNU" "Linux Programmer's Manual"
54 scanf, fscanf, sscanf, vscanf, vsscanf, vfscanf \- input format conversion
59 .BI "int scanf(const char *" format ", ...);"
60 .BI "int fscanf(FILE *" stream ", const char *" format ", ...);"
61 .BI "int sscanf(const char *" str ", const char *" format ", ...);"
63 .B #include <stdarg.h>
65 .BI "int vscanf(const char *" format ", va_list " ap );
66 .BI "int vsscanf(const char *" str ", const char *" format ", va_list " ap );
67 .BI "int vfscanf(FILE *" stream ", const char *" format ", va_list " ap );
71 Feature Test Macro Requirements for glibc (see
72 .BR feature_test_macros (7)):
80 _ISOC99_SOURCE || _POSIX_C_SOURCE\ >=\ 200112L
86 family of functions scans input according to
89 This format may contain
90 .IR "conversion specifications" ;
91 the results from such conversions, if any,
92 are stored in the locations pointed to by the
98 argument must be of a type that is appropriate for the value returned
99 by the corresponding conversion specification.
101 If the number of conversion specifications in
103 exceeds the number of
105 arguments, the results are undefined.
108 arguments exceeds the number of conversion specifications, then the excess
110 arguments are evaluated, but are otherwise ignored.
114 function reads input from the standard input stream
117 reads input from the stream pointer
121 reads its input from the character string pointed to by
126 function is analogous to
128 and reads input from the stream pointer
130 using a variable argument list of pointers (see
134 function scans a variable argument list from the standard input and the
136 function scans it from a string; these are analogous to the
140 functions respectively.
144 string consists of a sequence of
146 which describe how to process the sequence of input characters.
147 If processing of a directive fails, no further input is read, and
150 A "failure" can be either of the following:
151 .IR "input failure" ,
152 meaning that input characters were unavailable, or
153 .IR "matching failure" ,
154 meaning that the input was inappropriate (see below).
156 A directive is one of the following:
159 A sequence of white-space characters (space, tab, newline, etc.; see
161 This directive matches any amount of white space,
162 including none, in the input.
165 An ordinary character (i.e., one other than white space or \(aq%\(aq).
166 This character must exactly match the next character of input.
169 A conversion specification,
170 which commences with a \(aq%\(aq (percent) character.
171 A sequence of characters from the input is converted according to
172 this specification, and the result is placed in the corresponding
175 If the next item of input does not match the conversion specification,
176 the conversion fails\(emthis is a
177 .IR "matching failure" .
180 .I conversion specification
183 begins with either the character \(aq%\(aq or the character sequence
184 "\fB%\fP\fIn\fP\fB$\fP"
185 (see below for the distinction) followed by:
188 An optional \(aq*\(aq assignment-suppression character:
190 reads input as directed by the conversion specification,
191 but discards the input.
194 argument is required, and this specification is not
195 included in the count of successful assignments returned by
199 For decimal conversions, an optional quote character (\(aq).
200 This specifies that the input number may include thousands'
201 separators as defined by the
203 category of the current locale.
206 The quote character may precede or follow the \(aq*\(aq
207 assignment-suppression character.
210 An optional \(aqm\(aq character.
211 This is used with string conversions
215 and relieves the caller of the
216 need to allocate a corresponding buffer to hold the input: instead,
218 allocates a buffer of sufficient size,
219 and assigns the address of this buffer to the corresponding
221 argument, which should be a pointer to a
223 variable (this variable does not need to be initialized before the call).
224 The caller should subsequently
226 this buffer when it is no longer required.
229 An optional decimal integer which specifies the
230 .IR "maximum field width" .
231 Reading of characters stops either when this maximum is reached or
232 when a nonmatching character is found, whichever happens first.
233 Most conversions discard initial white space characters (the exceptions
235 and these discarded characters don't count toward the maximum field width.
236 String input conversions store a terminating null byte (\(aq\\0\(aq)
237 to mark the end of the input;
238 the maximum field width does not include this terminator.
242 .IR "type modifier character" .
245 type modifier is used with integer conversions such as
247 to specify that the corresponding
251 rather than a pointer to an
256 .I "conversion specifier"
257 that specifies the type of input conversion to be performed.
259 The conversion specifications in
261 are of two forms, either beginning with \(aq%\(aq or beginning with
262 "\fB%\fP\fIn\fP\fB$\fP".
263 The two forms should not be mixed in the same
265 string, except that a string containing
266 "\fB%\fP\fIn\fP\fB$\fP"
267 specifications can include
274 specifications, then these correspond in order with successive
278 "\fB%\fP\fIn\fP\fB$\fP"
279 form (which is specified in POSIX.1-2001, but not C99),
281 is a decimal integer that specifies that the converted input should
282 be placed in the location referred to by the
289 .I "type modifier characters"
290 can appear in a conversion specification:
293 Indicates that the conversion will be one of
294 \fBd\fP, \fBi\fP, \fBo\fP, \fBu\fP, \fBx\fP, \fBX\fP, or \fBn\fP
295 and the next pointer is a pointer to a
298 .I unsigned short int
305 but the next pointer is a pointer to a
308 .IR "unsigned char" .
313 but the next pointer is a pointer to an
317 This modifier was introduced in C99.
320 Indicates either that the conversion will be one of
321 \fBd\fP, \fBi\fP, \fBo\fP, \fBu\fP, \fBx\fP, \fBX\fP, or \fBn\fP
322 and the next pointer is a pointer to a
328 or that the conversion will be one of
329 \fBe\fP, \fBf\fP, or \fBg\fP
330 and the next pointer is a pointer to
336 characters is equivalent to
342 the corresponding parameter is considered
343 as a pointer to a wide character or wide-character string respectively.
344 .\" This use of l was introduced in Amendment 1 to ISO C90.
347 Indicates that the conversion will be either
348 \fBe\fP, \fBf\fP, or \fBg\fP
349 and the next pointer is a pointer to
351 or the conversion will be
352 \fBd\fP, \fBi\fP, \fBo\fP, \fBu\fP, or \fBx\fP
353 and the next pointer is a pointer to
355 .\" MTK, Jul 05: The following is no longer true for modern
356 .\" ANSI C (i.e., C99):
357 .\" (Note that long long is not an
359 .\" type. Any program using this will not be portable to all
365 This specifier does not exist in ANSI C.
370 but the next pointer is a pointer to a
372 This modifier was introduced in C99.
377 but the next pointer is a pointer to a
379 This modifier was introduced in C99.
382 .I "conversion specifiers"
386 Matches a literal \(aq%\(aq.
389 in the format string matches a
390 single input \(aq%\(aq character.
391 No conversion is done (but initial white space characters are discarded),
392 and assignment does not occur.
395 Matches an optionally signed decimal integer;
396 the next pointer must be a pointer to
402 this exists only for backward compatibility.
403 (Note: thus only in libc4.
404 In libc5 and glibc the
406 is silently ignored, causing old programs to fail mysteriously.)
409 Matches an optionally signed integer; the next pointer must be a pointer to
411 The integer is read in base 16 if it begins with
415 in base 8 if it begins with
417 and in base 10 otherwise.
418 Only characters that correspond to the base are used.
421 Matches an unsigned octal integer; the next pointer must be a pointer to
425 Matches an unsigned decimal integer; the next pointer must be a
430 Matches an unsigned hexadecimal integer; the next pointer must
439 Matches an optionally signed floating-point number; the next pointer must
460 Matches a sequence of non-white-space characters;
461 the next pointer must be a pointer to the initial element of a
462 character array that is long enough to hold the input sequence and
463 the terminating null byte (\(aq\\0\(aq), which is added automatically.
464 The input string stops at white space or at the maximum field
465 width, whichever occurs first.
468 Matches a sequence of characters whose length is specified by the
469 .I maximum field width
470 (default 1); the next pointer must be a pointer to
472 and there must be enough room for all the characters
473 (no terminating null byte is added).
474 The usual skip of leading white space is suppressed.
475 To skip white space first, use an explicit space in the format.
478 Matches a nonempty sequence of characters from the specified set of
479 accepted characters; the next pointer must be a pointer to
481 and there must be enough room for all the characters in the string, plus a
482 terminating null byte.
483 The usual skip of leading white space is suppressed.
484 The string is to be made up of characters in (or not in) a particular set;
485 the set is defined by the characters between the open bracket
487 character and a close bracket
492 those characters if the first character after the open bracket is a
495 To include a close bracket in the set, make it the first character after
496 the open bracket or the circumflex; any other position will end the set.
499 is also special; when placed between two other characters, it adds all
500 intervening characters to the set.
501 To include a hyphen, make it the last
502 character before the final close bracket.
506 the set "everything except close bracket, zero through nine, and hyphen".
507 The string ends with the appearance of a character not in the (or, with a
508 circumflex, in) set or when the field width runs out.
511 Matches a pointer value (as printed by
515 the next pointer must be a pointer to a pointer to
519 Nothing is expected; instead, the number of characters consumed thus far
520 from the input is stored through the next pointer, which must be a pointer
525 a conversion and does
527 increase the count returned by the function.
528 The assignment can be suppressed with the
530 assignment-suppression character, but the effect on the
531 return value is undefined.
534 conversions should not be used.
536 On success, these functions return the number of input items
537 successfully matched and assigned;
538 this can be fewer than provided for,
539 or even zero, in the event of an early matching failure.
543 is returned if the end of input is reached before either the first
544 successful conversion or a matching failure occurs.
546 is also returned if a read error occurs,
547 in which case the error indicator for the stream (see
551 is set to indicate the error.
555 The file descriptor underlying
557 is marked nonblocking, and the read operation would block.
560 The file descriptor underlying
562 is invalid, or not open for reading.
565 Input byte sequence does not form a valid character.
568 The read operation was interrupted by a signal; see
572 Not enough arguments; or
580 The result of an integer conversion would exceed the size
581 that can be stored in the corresponding integer type.
583 For an explanation of the terms used in this section, see
589 Interface Attribute Value
599 T} Thread safety MT-Safe locale
608 conform to C89 and C99 and POSIX.1-2001.
609 These standards do not specify the
615 specifier is the 4.4BSD notation for
621 in integer conversions is the GNU notation.
623 The Linux version of these functions is based on the
632 for a more concise description.
634 .SS The 'a' assignment-allocation modifier
635 Originally, the GNU C library supported dynamic allocation for string inputs
636 (as a nonstandard extension) via the
639 (This feature is present at least as far back as glibc 2.0.)
640 Thus, one could write the following to have
642 allocate a buffer for an input string,
643 with a pointer to that buffer being returned in
649 The use of the letter
651 for this purpose was problematic, since
653 is also specified by the ISO C standard as a synonym for
655 (floating-point input).
656 POSIX.1-2008 instead specifies the
658 modifier for assignment allocation (as documented in DESCRIPTION, above).
662 modifier is not available if the program is compiled with
665 .IR "gcc -D_ISOC99_SOURCE"
668 is also specified), in which case the
670 is interpreted as a specifier for floating-point numbers (see above).
674 modifier was added to glibc starting with version 2.7,
675 and new programs should use that modifier instead of
678 As well as being standardized by POSIX, the
680 modifier has the following further advantages over
684 It may also be applied to
686 conversion specifiers (e.g.,
689 It avoids ambiguity with respect to the
691 floating-point conversion specifier (and is unaffected by
695 All functions are fully C89 conformant, but provide the
696 additional specifiers
700 as well as an additional behavior of the
705 The latter may be considered to be a bug, as it changes the
706 behavior of specifiers defined in C89.
708 Some combinations of the type modifiers and conversion
709 specifiers defined by ANSI C do not make sense
712 While they may have a well-defined behavior on Linux, this need not
713 to be so on other architectures.
714 Therefore it usually is better to use
715 modifiers that are not defined by ANSI C at all, that is, use
720 \fBd\fP, \fBi\fP, \fBo\fP, \fBu\fP, \fBx\fP, and \fBX\fP
726 is not the same as on 4.4BSD,
727 as it may be used in float conversions equivalently to
730 To use the dynamic allocation conversion specifier, specify
732 as a length modifier (thus
735 \fB%m[\fP\fIrange\fP\fB]\fP).
738 the returned string, as in the following example:
746 n = scanf("%m[a-z]", &p);
748 printf("read: %s\\n", p);
750 } else if (errno != 0) {
753 fprintf(stderr, "No matching characters\\n");
758 As shown in the above example, it is necessary to call
762 call successfully read a string.