Reputation: 824
My problem is the following:
int c;
c = getchar();
while (c != EOF) {
putchar(c);
c = getchar();
}
while((c = getchar()) != EOF) {
putchar(c);
}
These two does the same thing. My question is how they both work? And why they both produce the same result?
As far as my understanding goes lets take the first one as example here, it reads one character, its not EOF-> go into while putchar(printchar on the screen) then move into get another char. However lets say i input something like "Hello", it will output Hello, how and why? Why does it not just output 5x H -> HHHHH ?
Upvotes: 0
Views: 1578
Reputation: 3
This is an old question, but I'll answer as I'm sure others will have the same question when studying the book, "The C Programming Language 2nd Edition". Which is how I stumbled upon this question myself. The following is what I discovered.
Char input is evaluated by the getchar() function, and the loop is entered. Once inside the loop putchar(c) is executed, and the standard output buffer is written to.
The reason that putchar(c) does not immediately print to the console the char is because of the way that the output buffer writes to the console. The out buffer does not write to the console until a newline int (\n) is encountered.
So if you type "Hello" on the console and press the enter key you are sending Hello\n to the program. Pressing the enter key sends the newline (\n) int value. Once the last int is evaluated by getchar() in the while loop you are picking up the newline (\n), and subsequently the \n new line int value is written to the buffer when putchar(c) is called. The buffer encountered \n which triggers the output buffer to write to the console all values stored in the output buffer up to that point.
Upvotes: 0
Reputation: 9681
The first loop
c = getchar();
while (c != EOF) {
putchar(c);
c = getchar();
}
assigns what's in stdin
inside the loops body while the second loop
while((c = getchar()) != EOF) {
putchar(c);
}
you combine the assignment with the comparison inside the loop's logical expression. This is due to the evaluation order in C and in many other languages (you can do the same in Java too). So you get the value of c
from the first term (which is expression in this case) and then compare it to EOF
.
Btw I would change the first loop to
do {
putchar(c);
c = getchar();
} while (c != EOF);
This way you don't have to pre-assign a value to c
and go straight for what putchar()
is delivering.
Upvotes: 0
Reputation: 29724
Simply.
You agree that
while (c != EOF) {
putchar(c);
c = getchar();
}
is equivalent to
while ((c = getchar()) != EOF) {
putchar(c);
}
for every call from the second up to the end, because it doesn't matter if you assign new values to c
at the end of loop and then comapre to EOF
or do it in one call in the loop stop condition.
But it's for all calls apart from the first one. If you left
int c;
c = getchar();
before the loop than for the first call you would evaluate c = getchar()
twice. So we must remove this and we are left with:
while((c = getchar()) != EOF) {
putchar(c);
}
Upvotes: 0
Reputation: 1147
putchar
is a function in the C programming language that writes a single character to the standard output stream, stdout.
The C library function int getchar(void)
gets a character (an unsigned char) from stdin. This is equivalent to getc
with stdin.
Hope this Helps.
Upvotes: 2