-
scanf problem
When I use the following method to input numbers, it is seen that the compiler /linker takes 11 inputs from the user.
for(i=0;i++;i<10)
scanf("%d", &a[i]);
However when i use the following way to scan the numbers, it is seen that the compiler/linker behaves as expected. i.e 10 inputs are taken from the user.
i=10;
while(i--)
scanf(""%d", &a[i]);
My question, is why the disparity. Why does the user have to input 11 numbers, despite running the loop 10 times.
-
for(initializers; condition; aux)
So you mean
Code:
for(i = 0; i < 10; i++)
scanf("%d", &a[i]);
You may also want to read the manual regarding scanf(), use sscanf() instead.
Other than that, the result is logical. while(i--) means basically: while((i - 1)) hence 10, instead of 11 as seen in the for loop... probably related to scanf.
Consider:
Code:
int i = 10;
do {
printf("loop %d\n", i);
} while(i--);
Notice the loop is run before i is touched,