Questions related to the scanf() family of functions in the C runtime library, which read and convert formatted data. (Includes scanf(), sscanf(), fscanf(), and their variadic equivalents.)
The scanf()
function reads data with specified format from stdin
. Originating from the C runtime library, scanf()
is present in many other programming languages.
The scanf()
function, in the C runtime library, reads input text for numbers and other data types from standard input. It returns the total number of items successfully matched, which can be less than the number requested. If the input stream is exhausted or reading from it otherwise fails before any items are matched, EOF is returned. The C prototype for scanf()
is as follows:
int scanf(const char *format, ...);
The specification of conversion identifiers in format
is a rich treasure of solutions—and occasional slight incompatibilities. The core functionality goes back to the beginnings of C
. All of the ...
parameters must be a pointer—note that an array name is a pointer type—to matching types of data.
Though popular for simple input in instructional programs, scanf()
does not handle errors or complicated situations well, and many programmers recommend using alternative input techniques.
The scanf tag is used for questions related to the use of the scanf()
, sscanf()
, and fscanf()
functions and their derivatives. sscanf()
reads from a string buffer; fscanf()
reads from a FILE *
; vscanf()
, vsscanf()
, vfscanf
do—respectively—the same using a va_list
instead of an explicit list of variables.
See also:
atoi atof strtol strtod
scanf
documentation.
scanf()
is the converse operation to printf and shares many of the same format specifiers.