When you type 1ENTER
the library code sees the 1
and matches it to "%d"
. Then it sees the ENTER
and starts matching that to " "
. As nothing else gets in, it waits and waits and waits.
After a while you type 2ENTER
. Since scanf()
is still waiting for whitespace and 2
is not whitespace the call terminates successfuly, leaving the 2
in the buffer and printing "The input was 1"
.
and so on ...
So, do not put spaces at the end of the conversion specification -- or anywhere else since most conversion specification already do leading whitespace suppresion.