I have a question about process that the scanf function takes when it encounters a non-whitespace character in the format string. According to the book I'm reading:
When a
scanf
function encounters a non-whitespace character in a format string,scanf
compares it with the next input character. If the two characters match,scanf
discards the input character and continues processing the format string. If the characters don't match,scanf
puts the offending character back into the input, then aborts without further processing the format string or reading characters from the input.
I am a little confused. It says that scanf
compares it with the next input character and if the two characters match, scanf
discards the input character. Why do we say that it compares with the "next" input character?
Does this mean that if we have a format string like
scanf("%d/%d", &x, &y)
and the input 2/4
, scanf
compares with the four because it's the next input character from the /
?
%d
reads2
, the next input character is/
and that gets compared with the/
from your format string. Then%d
reads4
. – mch