2 .\" Copyright 2022 Alejandro Colomar <alx@kernel.org>
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .TH scanf 3 (date) "Linux man-pages (unreleased)"
7 scanf, fscanf, vscanf, vfscanf \- input FILE format conversion
10 .RI ( libc ", " \-lc )
15 .BI "int scanf(const char *restrict " format ", ...);"
16 .BI "int fscanf(FILE *restrict " stream ,
17 .BI " const char *restrict " format ", ...);"
19 .B #include <stdarg.h>
21 .BI "int vscanf(const char *restrict " format ", va_list " ap );
22 .BI "int vfscanf(FILE *restrict " stream ,
23 .BI " const char *restrict " format ", va_list " ap );
27 Feature Test Macro Requirements for glibc (see
28 .BR feature_test_macros (7)):
34 _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L
39 family of functions scans formatted input like
43 It is very difficult to use these functions correctly,
44 and it is preferable to read entire lines with
48 and parse them later with
50 or more specialized functions such as
55 function reads input from the standard input stream
59 reads input from the stream pointer
64 function is analogous to
66 and reads input from the stream pointer
68 using a variable argument list of pointers (see
72 function is analogous to
74 and reads from the standard input.
76 On success, these functions return the number of input items
77 successfully matched and assigned;
78 this can be fewer than provided for,
79 or even zero, in the event of an early matching failure.
83 is returned if the end of input is reached before either the first
84 successful conversion or a matching failure occurs.
86 is also returned if a read error occurs,
87 in which case the error indicator for the stream (see
91 is set to indicate the error.
95 The file descriptor underlying
97 is marked nonblocking, and the read operation would block.
100 The file descriptor underlying
102 is invalid, or not open for reading.
105 Input byte sequence does not form a valid character.
108 The read operation was interrupted by a signal; see
112 Not enough arguments; or
119 For an explanation of the terms used in this section, see
125 Interface Attribute Value
133 T} Thread safety MT-Safe locale
140 These functions make it difficult to
141 distinguish newlines from other white space,
142 This is especially problematic with line-buffered input,
143 like the standard input stream.
145 These functions can't report errors after the last
146 non-suppressed conversion specification.
148 It is impossible to accurately know
149 how many characters these functions have consumed from the input stream,
150 since they only report the number of successful conversions.
152 if the input is "123\en\ a",
153 .I scanf(\[dq]%d\ %d\[dq], &a, &b)
154 will consume the digits, the newline, and the space, but not the letter a.
155 This makes it difficult to recover from invalid input.