From the horse's mouth:
7.21.6.2 The fscanf function
...
7 A directive that is a conversion specification defines a set of matching input sequences, as
described below for each specifier. A conversion specification is executed in the
following steps:
8 Input white-space characters (as specified by the isspace function) are skipped, unless
the specification includes a [, c, or n specifier.284)
9 An input item is read from the stream, unless the specification includes an n specifier. An
input item is defined as the longest sequence of input characters which does not exceed
any specified field width and which is, or is a prefix of, a matching input sequence.285)
The first character, if any, after the input item remains unread. If the length of the input
item is zero, the execution of the directive fails; this condition is a matching failure unless
end-of-file, an encoding error, or a read error prevented input from the stream, in which
case it is an input failure.
10 Except in the case of a % specifier, the input item (or, in the case of a %n directive, the
count of input characters) is converted to a type appropriate to the conversion specifier. If
the input item is not a matching sequence, the execution of the directive fails: this
condition is a matching failure. Unless assignment suppression was indicated by a *, the
result of the conversion is placed in the object pointed to by the first argument following
the format argument that has not already received a conversion result. If this object
does not have an appropriate type, or if the result of the conversion cannot be represented
in the object, the behavior is undefined.
...
12 The conversion specifiers and their meanings are:
d Matches an optionally signed decimal integer, whose format is the same as
expected for the subject sequence of the strtol function with the value 10
for
the base argument. The corresponding argument shall be a pointer to
signed integer.
...
284) These white-space characters are not counted against a specified field width.
285) fscanf pushes back at most one input character onto the input stream. Therefore, some sequences
that are acceptable to strtod, strtol, etc., are unacceptable to fscanf.
The processing for scanf
is exactly the same; the only difference is that scanf
always reads from standard input.
Examples:
Suppose you type SpaceSpaceSpace123Enter in response to the first prompt; the input stream then contains the sequence {' ', ' ', ' ', '1', '2', '3', '\n'}
. When you call scanf( "%d", &n );
, scanf
reads and discards the leading blank spaces, then reads and matches the sequence {'1', '2', '3'}
, converts it to the integer value 123
, and assigns the result to n
. Since there was a successful conversion and assignment, scanf
returns 1.
If the input stream contains the sequence {' ', ' ', ' ', '1', '2', '.', '3', '\n'}
, scanf
reads and discards the leading blanks, then reads and matches the sequence {'1', '2'}
, converts it to the integer value 12
, and assigns the result to n
. The input stream will still contain {'.', '3', '\n'}
. Since there was a successful conversion and assignment, scanf
will return 1.
If the input stream contains the sequence {'.', '3', '\n'}
, then there is no matching sequence of characters ('.'
is not a valid character in a decimal integer). scanf
will leave the .
unread and leave the value of n
unchanged. Since there was not a successful conversion and assignment, scanf
returns 0 to indicate a matching failure.
If an end-of-file is signaled on the input stream before any matching characters have been read, or if there's some other input error, scanf
does not assign any new value to n
and returns EOF
to indicate an input failure.