I'm very sorry for asking this silly question and also, I've checked in the threads related to this post still found no help. In the code snippet below:
#include <stdio.h>
main()
{
int c;
while((c=getchar()) != EOF)
putchar(c);
}
Q1: If I input hello
, the output is hello
. Shouldn't it just be h
because although getchar can read entire string but it'll accept only one character and thus produce the output as h
?
Q2: Assume I input n
, shouldn't it proceed as below:
getchar()
only consumes one character from the input stream. All of the other characters remain in the input stream for subsequent examination.
The first time you call getchar()
, it returns 'h'
. The second time, it returns 'e'
, then 'l'
, and so on. The sixth time it is invoked, it returns the end-of-line indicator '\\n'
. The seventh time it is invoked, it pauses, waiting for the user to type more data.
Only when the user indicates end of file will the function return EOF. If the input is from a terminal, the user might indicate end of file by typing CTRL + D (Unix, Linux) or CTRL + Z (DOS, Windows). If the input is from a disk file, end of file is indicated after all of the file's content is exhausted.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.