Reputation: 107
I am trying to fill my int array with numbers I get from the user.
but when I try to printf
a specific index that is bigger then, it shows the wrong number.
for(B_index=0,checker=0; B_index<SIZE&&!checker; B_index++)
{
checker=scanf("%d",&B[B_index]);
if(checker==EOF)
{
checker=1;
}
else if(checker<1)
{
printf("error");
return 1;
}
}
printf("%d",B[1]);
Upvotes: 0
Views: 156
Reputation: 13
scanf returns the number of assignments back, so if you entered a valid integer on the console, scanf will return 1, which means it assigned an integer (due to %d) to B[0]. This causes checker to be 1 and will hence break the loop immediately after the first number has been entered. B[1] is unassigned and as such will contain garbage or 0 if you cleared out the array.
Upvotes: 1