For loop iteration issue
Hey so i have tried many variations of this code including using a scanf
function and every time it increments by 2 points instead of one. Here is
the code.
#include <stdio.h>
int main(void)
{
double nc;
for(nc = 0; getchar() != EOF; ++nc)
printf("%.0f\n", nc);
}
This is the output i get. the input i used was qwerty, and the outputs are
numbers 0-11 instead of 0-5 as expected.
q 0 1 w 2 3 e 4 5 r 6 7 t 8 9 y 10 11
one thought i has was that when i press enter, it is counted as a value
for getchar along with the character i enter and this causes the loop to
run through 2 iterations? can further explain this concept or provide
links to more information about it for me, thanks.
No comments:
Post a Comment