After reading the original question, I think I understand what you are asking.
So in the other question the input was:
5<ENTER>
1 2 3 4 99<ENTER>
scanf
is a function design to read formatted input (the name comes from scan
formatted), it is not design to read random user input.
To quote the man page of scanf:
man scanf
#include <stdio.h>
int scanf(const char *format, ...);
DESCRIPTION
The scanf()
family of functions scans input according to format
as described below. This format may contain conversion specifications
;
the results from such conversions, if any, are stored in the locations pointed to by the pointer arguments that follow format. Each
pointer argument must be of a type that is appropriate for the value returned by the corresponding conversion specification.
That means that scanf scans the input according to the provided format. The
conversion specifier %d
matches an optionally signed decimal integer. If
your format contains only "%d"
it will consume any white-space characters and
try to convert the next sequence of non-white-space characters into an signed int
. If this is possible, scanf
will
store the converted integer in the memory pointed to by the passed argument. If
it fails at converting, it stops scanning and any other character in the input
buffer will remain in the input buffer.
So let's to this: You enter 3 numbers like this:
3SPACESPACE4SPACETAB5ENTER
Your input buffer will look like this:
+---+---+---+---+---+----+---+----+
| 3 | █ | █ | 4 | █ | \t | 5 | \n |
+---+---+---+---+---+----+---+----+
In the first iteration scanf
consume any white-space characters, right now
there is none, so it will read 3 and continue with the next character
which is a space. At that point it stops and converts the 3 and stores it in
&v[i]
. The input buffer will look like this:
+---+---+---+---+----+---+----+
| █ | █ | 4 | █ | \t | 5 | \n |
+---+---+---+---+----+---+----+
In the second iteration it will consume any white-space characters. There are
two spaces there and those are just consumed. Then it will read 4, and then a
white-space character. scanf
stops and and converts the 4 and stores it in
&v[i]
. The input buffer will look like this:
+---+----+---+----+
| █ | \t | 5 | \n |
+---+----+---+----+
As you can see, in every iteration scanf
didn't wait block and wait for the
user to enter something, because characters were left in the input buffer from
the previous iterations.
And that's why you can have scanf("%d", &v[i])
in a loop for an input like
this "1 2 3 4 5"
.