scanf, fscanf, sscanf - formatted input conversion
scanf (format [ , pointer ] ... )
fscanf (stream, format [ , pointer ] ... )
sscanf (s, format [ , pointer ] ... )
char *s, *format;
reads from the standard input stream
reads from the named input
reads from the character string
Each function reads characters, interprets them according to a format,
and stores the results in its arguments. Each expects, as arguments,
a control string
described below, and a set of
arguments indicating where the converted input should be stored.
The control string usually contains conversion specifications, which
are used to direct interpretation of input sequences. The control
string may contain:
Blanks, tabs, or new-lines, which cause input to be read up to the
next non-white-space character.
An ordinary character (not
), which must match the next character of the input stream.
Conversion specifications, consisting of the character
, an optional assignment suppressing character
, an optional numerical maximum field width, and a conversion
A conversion specification directs the conversion of the next input
field; the result is placed in the variable pointed to by the
corresponding argument, unless assignment suppression was indicated by
An input field is defined as a string of non-space characters; it
extends to the next inappropriate character or until the field width,
if specified, is exhausted.
The conversion character indicates the interpretation of the input
field; the correspondindg pointer argument must usually be of a
restricted type. The following characters are legal:
is expected in the input at this point; no assignment is done.
a decimal integer is expected; the corresponding argument should be an
an octal integer is expected; the corresponding argument should be an
a hexadecimal integer is expected; the corresponding argument
should be an integer pointer.
a character string is expected; the corresponding argument should be a
character pointer pointing to an array of characters large enough to
accept the string and a terminating \0, which will be added
automatically. The input field is terminated by a space character or
a character is expected; the corresponding argument should be
a character pointer. The normal skip over space characters is
suppressed in this case; to read the next non-space character, use
If a field width is given, the corresponding arguments should refer to
a character array; the indicated number of characters is read.
a floating point number is expected;the next field is converted
accordingly and stored through the corresponding argument, whcih
should be a pointer to a float. The input format for floating point
numbers is an optionally signed string of digits, possibly containing
a decimal point, followed by an optional exponent field consisting of
, followed by an optionally signed integer.
indicates a string that is not to be delimited by space
characters. The left bracket is followed by a set of characters and a
right bracket; the characters between the brackets define a set of
characters making up the string. If the first character is not a
circumflex (^), the input field consists of all characters up to the
first character that is not in the set between the brackets; if the
first character after the left bracket is a ^, the input field
consists of all characters up to the first character that is in the
set of the remaining characters between the brackets. The
corresponding argument must point to a character array.
The conversion characters
may be capitalized and/or preceded by
to indicate a pointer to long ranther than to int is in the argument
list. Similarly, the conversion characters
may be capitalized and/or preceded by
to indicate that a pointer to double rather than to float is in the
argument list. The character
will, some time in the future, indicate short data items.
conversion terminates at EOF, at the end of the control string, or
when an input character conflicts with the control string. In the
latter case, the offending character is left unread in the input
returns the number of successfully matched and assigned input items;
this number can be zero in the event of an early conflict between an
input character and the control string. If the input ends before the
first conflict or coversion., EOF is returned.
int i; float x; char name;
scanf ("%d%f%s", &i, &x, name);
with the input line:
25 54.32E-1 thompson
will assign to
the value 25, to
the value 5.432, and
will contain thompson\0. Or:
int i; float x; char ;
scanf (%2d%f%*d%[1234567890[", &i, &x, name);
56789 0123 56a72
will assign 56 to
skip 0123, and place the string 56\0 in
The next call to getchar (see
) will return
These functions return EOF on end of input and a short count for
missing or illegal data items.
The success of literal matches and suppressed assignments is not