The format string in scanf works as follows (see http://www.cplusplus.com/reference/cstdio/scanf/)
[The format is a] C string that contains a sequence of characters that control how
characters extracted from the stream are treated:
Whitespace
character: the function will read and ignore any whitespace characters
encountered before the next non-whitespace character ...
In both your scanf()
you have a newline. Therefore the first time you hit the enter key, it is ignored by scanf
.
Some of the answers are telling you to modify the loop... this is incorrect, your loop is fine. it is the above that's causing you the headache. Try the following:
#include <stdio.h>
int main(int argc, char const *argv[])
{
int T, y, z;
scanf ("%i", &T);
printf("Count is= %d\n", T);
for (T; T > 0 ; --T)
{
printf("T= %d\n", T);
scanf ("%i", &y);
}
return 0;
}
EDIT: Thank you to Daniel Fischer for his comment about flushing stdin, which I have now removed. Found this explanation (Using fflush(stdin)).