I'm new to C, sorry if this question is basic. I'm trying to understand the behaviour of the getchar()
function.
Here I have two versions of my code:
The first one:
#include <stdio.h>
int main()
{
int c = getchar();
while (c != EOF)
{
putchar(c);
c = getchar();
printf(" hello\n");
}
}
when I enter 12 and hit the return key this one produces:
12
1 hello
2 hello
and then there's the other one where I move printf() up, enter the same input
#include <stdio.h>
int main()
{
int c = getchar();
while (c != EOF)
{
putchar(c);
printf(" hello\n");
c = getchar();
}
}
and it produces:
12
1 hello
2 hello
hello
Why won't these two work the same way and why does that extra hello appear at the end of the second code.