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 2014-01-11 "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 _XOPEN_SOURCE\ >=\ 600 || _ISOC99_SOURCE ||
81 _POSIX_C_SOURCE\ >=\ 200112L;
90 family of functions scans input according to
93 This format may contain
94 .IR "conversion specifications" ;
95 the results from such conversions, if any,
96 are stored in the locations pointed to by the
102 argument must be of a type that is appropriate for the value returned
103 by the corresponding conversion specification.
105 If the number of conversion specifications in
107 exceeds the number of
109 arguments, the results are undefined.
112 arguments exceeds the number of conversion specifications, then the excess
114 arguments are evaluated, but are otherwise ignored.
118 function reads input from the standard input stream
121 reads input from the stream pointer
125 reads its input from the character string pointed to by
130 function is analogous to
132 and reads input from the stream pointer
134 using a variable argument list of pointers (see
138 function scans a variable argument list from the standard input and the
140 function scans it from a string; these are analogous to the
144 functions respectively.
148 string consists of a sequence of
150 which describe how to process the sequence of input characters.
151 If processing of a directive fails, no further input is read, and
154 A "failure" can be either of the following:
155 .IR "input failure" ,
156 meaning that input characters were unavailable, or
157 .IR "matching failure" ,
158 meaning that the input was inappropriate (see below).
160 A directive is one of the following:
163 A sequence of white-space characters (space, tab, newline, etc.; see
165 This directive matches any amount of white space,
166 including none, in the input.
169 An ordinary character (i.e., one other than white space or \(aq%\(aq).
170 This character must exactly match the next character of input.
173 A conversion specification,
174 which commences with a \(aq%\(aq (percent) character.
175 A sequence of characters from the input is converted according to
176 this specification, and the result is placed in the corresponding
179 If the next item of input does not match the conversion specification,
180 the conversion fails\(emthis is a
181 .IR "matching failure" .
184 .I conversion specification
187 begins with either the character \(aq%\(aq or the character sequence
188 "\fB%\fP\fIn\fP\fB$\fP"
189 (see below for the distinction) followed by:
192 An optional \(aq*\(aq assignment-suppression character:
194 reads input as directed by the conversion specification,
195 but discards the input.
198 argument is required, and this specification is not
199 included in the count of successful assignments returned by
203 An optional \(aqm\(aq character.
204 This is used with string conversions
208 and relieves the caller of the
209 need to allocate a corresponding buffer to hold the input: instead,
211 allocates a buffer of sufficient size,
212 and assigns the address of this buffer to the corresponding
214 argument, which should be a pointer to a
216 variable (this variable does not need to be initialized before the call).
217 The caller should subsequently
219 this buffer when it is no longer required.
222 An optional decimal integer which specifies the
223 .IR "maximum field width" .
224 Reading of characters stops either when this maximum is reached or
225 when a nonmatching character is found, whichever happens first.
226 Most conversions discard initial white space characters (the exceptions
228 and these discarded characters don't count toward the maximum field width.
229 String input conversions store a terminating null byte (\(aq\\0\(aq)
230 to mark the end of the input;
231 the maximum field width does not include this terminator.
235 .IR "type modifier character" .
238 type modifier is used with integer conversions such as
240 to specify that the corresponding
244 rather than a pointer to an
249 .I "conversion specifier"
250 that specifies the type of input conversion to be performed.
252 The conversion specifications in
254 are of two forms, either beginning with \(aq%\(aq or beginning with
255 "\fB%\fP\fIn\fP\fB$\fP".
256 The two forms should not be mixed in the same
258 string, except that a string containing
259 "\fB%\fP\fIn\fP\fB$\fP"
260 specifications can include
267 specifications, then these correspond in order with successive
271 "\fB%\fP\fIn\fP\fB$\fP"
272 form (which is specified in POSIX.1-2001, but not C99),
274 is a decimal integer that specifies that the converted input should
275 be placed in the location referred to by the
282 .I "type modifier characters"
283 can appear in a conversion specification:
286 Indicates that the conversion will be one of
287 \fBd\fP, \fBi\fP, \fBo\fP, \fBu\fP, \fBx\fP, \fBX\fP, or \fBn\fP
288 and the next pointer is a pointer to a
291 .I unsigned short int
298 but the next pointer is a pointer to a
301 .IR "unsigned char" .
306 but the next pointer is a pointer to an
310 This modifier was introduced in C99.
313 Indicates either that the conversion will be one of
314 \fBd\fP, \fBi\fP, \fBo\fP, \fBu\fP, \fBx\fP, \fBX\fP, or \fBn\fP
315 and the next pointer is a pointer to a
321 or that the conversion will be one of
322 \fBe\fP, \fBf\fP, or \fBg\fP
323 and the next pointer is a pointer to
329 characters is equivalent to
335 the corresponding parameter is considered
336 as a pointer to a wide character or wide-character string respectively.
337 .\" This use of l was introduced in Amendment 1 to ISO C90.
340 Indicates that the conversion will be either
341 \fBe\fP, \fBf\fP, or \fBg\fP
342 and the next pointer is a pointer to
344 or the conversion will be
345 \fBd\fP, \fBi\fP, \fBo\fP, \fBu\fP, or \fBx\fP
346 and the next pointer is a pointer to
348 .\" MTK, Jul 05: The following is no longer true for modern
349 .\" ANSI C (i.e., C99):
350 .\" (Note that long long is not an
352 .\" type. Any program using this will not be portable to all
358 This specifier does not exist in ANSI C.
363 but the next pointer is a pointer to a
365 This modifier was introduced in C99.
370 but the next pointer is a pointer to a
372 This modifier was introduced in C99.
375 .I "conversion specifiers"
379 Matches a literal \(aq%\(aq.
382 in the format string matches a
383 single input \(aq%\(aq character.
384 No conversion is done (but initial white space characters are discarded),
385 and assignment does not occur.
388 Matches an optionally signed decimal integer;
389 the next pointer must be a pointer to
395 this exists only for backward compatibility.
396 (Note: thus only in libc4.
397 In libc5 and glibc the
399 is silently ignored, causing old programs to fail mysteriously.)
402 Matches an optionally signed integer; the next pointer must be a pointer to
404 The integer is read in base 16 if it begins with
408 in base 8 if it begins with
410 and in base 10 otherwise.
411 Only characters that correspond to the base are used.
414 Matches an unsigned octal integer; the next pointer must be a pointer to
418 Matches an unsigned decimal integer; the next pointer must be a
423 Matches an unsigned hexadecimal integer; the next pointer must
432 Matches an optionally signed floating-point number; the next pointer must
453 Matches a sequence of non-white-space characters;
454 the next pointer must be a pointer to character array that is
455 long enough to hold the input sequence and
456 the terminating null byte (\(aq\\0\(aq), which is added automatically.
457 The input string stops at white space or at the maximum field
458 width, whichever occurs first.
461 Matches a sequence of characters whose length is specified by the
462 .I maximum field width
463 (default 1); the next pointer must be a pointer to
465 and there must be enough room for all the characters
466 (no terminating null byte is added).
467 The usual skip of leading white space is suppressed.
468 To skip white space first, use an explicit space in the format.
471 Matches a nonempty sequence of characters from the specified set of
472 accepted characters; the next pointer must be a pointer to
474 and there must be enough room for all the characters in the string, plus a
475 terminating null byte.
476 The usual skip of leading white space is suppressed.
477 The string is to be made up of characters in (or not in) a particular set;
478 the set is defined by the characters between the open bracket
480 character and a close bracket
485 those characters if the first character after the open bracket is a
488 To include a close bracket in the set, make it the first character after
489 the open bracket or the circumflex; any other position will end the set.
492 is also special; when placed between two other characters, it adds all
493 intervening characters to the set.
494 To include a hyphen, make it the last
495 character before the final close bracket.
499 the set "everything except close bracket, zero through nine, and hyphen".
500 The string ends with the appearance of a character not in the (or, with a
501 circumflex, in) set or when the field width runs out.
504 Matches a pointer value (as printed by
508 the next pointer must be a pointer to a pointer to
512 Nothing is expected; instead, the number of characters consumed thus far
513 from the input is stored through the next pointer, which must be a pointer
518 a conversion, although it can be suppressed with the
520 assignment-suppression character.
521 The C standard says: "Execution of a
523 directive does not increment
524 the assignment count returned at the completion of execution"
525 but the Corrigendum seems to contradict this.
527 not to make any assumptions on the effect of
529 conversions on the return value.
531 These functions return the number of input items
532 successfully matched and assigned,
533 which can be fewer than provided for,
534 or even zero in the event of an early matching failure.
538 is returned if the end of input is reached before either the first
539 successful conversion or a matching failure occurs.
541 is also returned if a read error occurs,
542 in which case the error indicator for the stream (see
546 is set indicate the error.
550 The file descriptor underlying
552 is marked nonblocking, and the read operation would block.
555 The file descriptor underlying
557 is invalid, or not open for reading.
560 Input byte sequence does not form a valid character.
563 The read operation was interrupted by a signal; see
567 Not enough arguments; or
575 The result of an integer conversion would exceed the size
576 that can be stored in the corresponding integer type.
583 conform to C89 and C99 and POSIX.1-2001.
584 These standards do not specify the
590 specifier is the 4.4BSD notation for
596 in integer conversions is the GNU notation.
598 The Linux version of these functions is based on the
607 for a more concise description.
609 .SS The 'a' assignment-allocation modifier
610 Originally, the GNU C library supported dynamic allocation for string inputs
611 (as a nonstandard extension) via the
614 (This feature is present at least as far back as glibc 2.0.)
615 Thus, one could write the following to have
617 allocate a buffer for an input string,
618 with a pointer to that buffer being returned in
624 The use of the letter
626 for this purpose was problematic, since
628 is also specified by the ISO C standard as a synonym for
630 (floating-point input).
631 POSIX.1-2008 instead specifies the
633 modifier for assignment allocation (as documented in DESCRIPTION, above).
637 modifier is not available if the program is compiled with
640 .IR "gcc -D_ISOC99_SOURCE"
643 is also specified), in which case the
645 is interpreted as a specifier for floating-point numbers (see above).
649 modifier was added to glibc starting with version 2.7,
650 and new programs should use that modifier instead of
653 As well as being standardized by POSIX, the
655 modifier has the following further advantages over
659 It may also be applied to
661 conversion specifiers (e.g.,
664 It avoids ambiguity with respect to the
666 floating-point conversion specifier (and is unaffected by
670 All functions are fully C89 conformant, but provide the
671 additional specifiers
675 as well as an additional behavior of the
680 The latter may be considered to be a bug, as it changes the
681 behavior of specifiers defined in C89.
683 Some combinations of the type modifiers and conversion
684 specifiers defined by ANSI C do not make sense
687 While they may have a well-defined behavior on Linux, this need not
688 to be so on other architectures.
689 Therefore it usually is better to use
690 modifiers that are not defined by ANSI C at all, that is, use
695 \fBd\fP, \fBi\fP, \fBo\fP, \fBu\fP, \fBx\fP, and \fBX\fP
701 is not the same as on 4.4BSD,
702 as it may be used in float conversions equivalently to
705 To use the dynamic allocation conversion specifier, specify
707 as a length modifier (thus
710 \fB%m[\fP\fIrange\fP\fB]\fP).
713 the returned string, as in the following example:
721 n = scanf("%m[a-z]", &p);
723 printf("read: %s\\n", p);
725 } else if (errno != 0) {
728 fprintf(stderr, "No matching characters\\n");
733 As shown in the above example, it is necessary to call
737 call successfully read a string.
746 This page is part of release 3.79 of the Linux
749 A description of the project,
750 information about reporting bugs,
751 and the latest version of this page,
753 \%http://www.kernel.org/doc/man\-pages/.