Sandrine
2011-03-18 13:30:32 UTC
#include
main()
{
int c;
c = getchar();
while(c != EOF){
putchar(c);
c = getchar();
}
}
My first question is what is the flow of the script?
My understanding is:
When I first run the script, I am in the main() function. The script wait for an input. I press "1". Once the first input is entered by pressing the key "1", the value is stored in the parameter 'c' (c=getchar()) // because c=1 is not EOF, the value of c is print on the screen (putchar(), then the 2nd input, let's say "2", is read and the value is stored in c and compared to EOF and print on the screen.
When I run the script, this is actually what I see on the cmd window: I start by the input "12'. I then pressed 'enter' key, and all the inputs are copied on the second line i.e "12", and the command prompt window waits for additional inputs on the 3rd line.
--> Why is the output printed on the screen only after I pressed "ENTER"?
There is nothing in the script above that says to print the output after "ENTER" key is pressed.
--> The way I read the script, I would have expected that the output is printed everytime I press a key. So when I press "1", I shoudl see on the screen "11" (the first "1" is the input, and the 2nd "1" is the copy from putchar()).
If I replace EOF by '\n', then same thing happen, except that instead of being on the 3rd line waiting for additional entry, the program stops and the prompt show me the adress (C:\).