Is there any reason of the second 'c = getchar()' mention in this code example?
#include <stdio.h>
/* copy input to output; 1st version */
int main(void) {
int c;
c = getchar();
while (c != EOF) {
putchar(c);
c = getchar(); // <-- I mean this one.
}
return 0;
}
c = getchar(); //read for the first time before entering while loop
while (c != EOF) {
putchar(c);
c = getchar(); // read the next input and go back to condition checking
}
return 0;
getchar()
reads the first time input character.getchar()
keeps on reading next input(s), untill a EOF
In other words, the purpose of while (c != EOF)
is to keep on checking whether the c
is EOF
or not. if c
is not changed, then the while()
loop is meaningless, isn't it? The second getch()
is responsible for chnaging the value of c
in each iteration.