You are calling getc
twice per iteration of the loop; one of these two calls compares the character to EOF
, while the other call compares the character to ' '
.
This has two consequences:
- Your program will only print
"up"
for the spaces which are on even position, and will miss all spaces which are on odd position;
- Your program might make one extra call to
getc
after reaching EOF
the first time.
How to fix
You need to make a single call to getc
per iteration of the loop. Save the character returned by getc
to a local variable; then use this variable to check for spaces in the body of the loop, and to check for EOF
in the condition of the loop.