SCANF(3) Linux Programmer's Manual SCANF(3)
NAME
scanf, fscanf, sscanf, vscanf, vsscanf, vfscanf - input format conversion
SYNOPSIS
#include <stdio.h>
int scanf(const char *format, ...);
int fscanf(FILE *stream, const char *format, ...);
int sscanf(const char *str, const char *format, ...);
#include <stdarg.h>
int vscanf(const char *format, va_list ap);
int vsscanf(const char *str, const char *format, va_list ap);
int vfscanf(FILE *stream, const char *format, va_list ap);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
vscanf(), vsscanf(), vfscanf():
_XOPEN_SOURCE >= 600 || _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L;
or cc -std=c99
DESCRIPTION
The scanf() family of functions scans input according to format as described below. This format may contain conversion specifications; the
results from such conversions, if any, are stored in the locations pointed to by the pointer arguments that follow format. Each pointer
argument must be of a type that is appropriate for the value returned by the corresponding conversion specification.
If the number of conversion specifications in format exceeds the number of pointer arguments, the results are undefined. If the number of
pointer arguments exceeds the number of conversion specifications, then the excess pointer arguments are evaluated, but are otherwise
ignored.
The scanf() function reads input from the standard input stream stdin, fscanf() reads input from the stream pointer stream, and sscanf()
reads its input from the character string pointed to by str.
The vfscanf() function is analogous to vfprintf(3) and reads input from the stream pointer stream using a variable argument list of point-
ers (see stdarg(3). The vscanf() function scans a variable argument list from the standard input and the vsscanf() function scans it from
a string; these are analogous to the vprintf(3) and vsprintf(3) functions respectively.
The format string consists of a sequence of directives which describe how to process the sequence of input characters. If processing of a
directive fails, no further input is read, and scanf() returns. A "failure" can be either of the following: input failure, meaning that
input characters were unavailable, or matching failure, meaning that the input was inappropriate (see below).
A directive is one of the following:
o A sequence of white-space characters (space, tab, newline, etc.; see isspace(3)). This directive matches any amount of white space,
including none, in the input.
o An ordinary character (i.e., one other than white space or '%'). This character must exactly match the next character of input.
o A conversion specification, which commences with a '%' (percent) character. A sequence of characters from the input is converted
according to this specification, and the result is placed in the corresponding pointer argument. If the next item of input does not
match the conversion specification, the conversion fails--this is a matching failure.
Each conversion specification in format begins with either the character '%' or the character sequence "%n$" (see below for the distinc-
tion) followed by:
o An optional '*' assignment-suppression character: scanf() reads input as directed by the conversion specification, but discards the
input. No corresponding pointer argument is required, and this specification is not included in the count of successful assignments
returned by scanf().
o An optional 'a' character. This is used with string conversions, and relieves the caller of the need to allocate a corresponding
buffer to hold the input: instead, scanf() allocates a buffer of sufficient size, and assigns the address of this buffer to the cor-
responding pointer argument, which should be a pointer to a char * variable (this variable does not need to be initialized before
the call). The caller should subsequently free(3) this buffer when it is no longer required. This is a GNU extension; C99 employs
the 'a' character as a conversion specifier (and it can also be used as such in the GNU implementation).
o An optional decimal integer which specifies the maximum field width. Reading of characters stops either when this maximum is
reached or when a nonmatching character is found, whichever happens first. Most conversions discard initial white space characters
(the exceptions are noted below), and these discarded characters don't count toward the maximum field width. String input conver-
sions store a terminating null byte ('