ECE209_scanf.pptx
ECE 209 Fall 2014 streams & scanf
I/O Stream
The standard C I/O functions operate on a
“stream” of bytes (usually characters).
• Printf produces a sequence of bytes to an output stream.
• Scanf consumes a sequence of bytes from an input stream.
ECE 209 Fall 2014 streams & scanf
Scanf consumes from stdin
The stream called stdin is the sequence of characters
typed on the keyboard.
• Small caveat: Characters are usually buffered by the OS,
so scanf doesn’t “see” characters until linefeed is pressed.
abc\n123,456\n…
Only “sees” the first character on the stream.
It either consumes it, or leaves it on the stream.
ECE 209 Fall 2014 streams & scanf
Scanf: format string
The format string tells scanf what to look for,
and what to do with the characters it consumes.
The format string may contain:
• Whitepace character – skips any whitespace characters
in the stream.
• Ordinary characters (not %) – must match the next
character in the stream.
• Conversion specifications, starting with % – converts a
sequence of characters into a value, string, etc., and stores
to memory, using the next argument (which must be a pointer).
ECE 209 Fall 2014 streams & scanf
Example: %d
scanf(“%d”, ptr );
400\nabc\n123…
-3
ECE 209 Fall 2014 streams & scanf
Example: %d
scanf(“%d”, ptr );
\nabc\n123…
(1) Consumes and skips spaces.
(2) Sees ‘4’, removes it from stream.
(3) Sees ‘0’, removes it from stream.
(4) Sees ‘0’, removes it from stream.
(5) Sees ‘\n’, leaves on stream because it’s not a decimal digit.
(6) Converts “400” to decimal value 400 and stores to *ptr.
400
ECE 209 Fall 2014 streams & scanf
Example: %d
scanf(“%d”, ptr );
abc\n123…
(1) Sees ‘a’ and leaves it on the stream, because it’s not a decimal digit.
(Nothing is stored to *ptr.)
How does caller know whether a value was read
and stored to *ptr?
-3
ECE 209 Fall 2014 streams & scanf
Scanf return value
Scanf is a function that returns an int.
The return value is the number of values that were
converted and stored.
scanf(“%d”, ptr);
400\nabc\n123…
scanf(“%d”, ptr);
abc\n123…
Returns 1.
Returns 0.
ECE 209 Fall 2014 streams & scanf
Example: multiple fields
Suppose our program wants to read a time value,
that looks like HH:MM.
scanf(“%d:%d”, &hours, &mins);
12:45\n…
ECE 209 Fall 2014 streams & scanf
Example: multiple fields
Suppose our program wants to read a time value,
that looks like HH:MM.
scanf(“%d:%d”, &hours, &mins);
12:45\n…
(1) Looking for decimal #.
ECE 209 Fall 2014 streams & scanf
Example: multiple fields
Suppose our program wants to read a time value,
that looks like HH:MM.
scanf(“%d:%d”, &hours, &mins);
\n…
(1) Looking for decimal #: reads “12”, converts to 12, and stores it.
(2) Looking for ‘:’. Sees it, removes it from stream.
(3) Looking for decimal #: reads “45”, converts to 45, and stores it.
Returns 2.
ECE 209 Fall 2014 streams & scanf
Example: multiple fields
Suppose our program wants to read a time value,
that looks like HH:MM.
scanf(“%d:%d”, &hours, &mins);
12/45\n…
ECE 209 Fall 2014 streams & scanf
Example: multiple fields
Suppose our program wants to read a time value,
that looks like HH:MM.
scanf(“%d:%d”, &hours, &mins);
/45\n…
(1) Looking for decimal #. Reads “12” and stores 12.
(2) Looking for ‘:’ – doesn’t see it.
Returns 1.
ECE 209 Fall 2014 streams & scanf
Example
Suppose you want to read the next integer from
stdin, but there might be other characters before
the next integer…
while (scanf(“%d”, &n) != 1) {
/* remove a character and try again */
scanf(“%c”, &bogus);
}
while (scanf(“%d”, &n) != 1) {
/* remove a character and try again */
scanf(“%*c”);
}
NOTE: If there are multiple conversion specs in the format string,
scanf returns as soon as one does not work; it doesn’t “move on”
to the next one.