`getchar()` gives the same output as the input string
In the simple setup you are likely using, getchar
works with buffered input, so you have to press enter before getchar gets anything to read. Strings are not terminated by EOF
; in fact, EOF
is not really a character, but a magic value that indicates the end of the file. But EOF
is not part of the string read. It's what getchar
returns when there is nothing left to read.
Strings, by C
definition, are terminated by '\0'
. You have no "C strings"
in your program.
Your program reads characters (buffered till ENTER) from the standard input (the keyboard) and writes them back to the standard output (the screen). It does this no matter how many characters you type or for how long you do this.
To stop the program you have to indicate that the standard input has no more data (huh?? how can a keyboard have no more data?).
You simply press Ctrl+D (Unix) or Ctrl+Z (Windows) to pretend the file has reached its end.
Ctrl+D (or Ctrl+Z) are not really characters in the C
sense of the word.
If you run your program with input redirection, the EOF
is the actual end of file, not a make belief one./a.out < source.c
There is an underlying buffer/stream that getchar()
and friends read from. When you enter text, the text is stored in a buffer somewhere. getchar()
can stream through it one character at a time. Each read returns the next character until it reaches the end of the buffer. The reason it's not asking you for subsequent characters is that it can fetch the next one from the buffer.
If you run your script and type directly into it, it will continue to prompt you for input until you press CTRL+D (end of file). If you call it like ./program < myInput
where myInput
is a text file with some data, it will get the EOF
when it reaches the end of the input. EOF
isn't a character that exists in the stream, but a sentinel value to indicate when the end of the input has been reached.
As an extra warning, I believe getchar()
will also return EOF
if it encounters an error, so you'll want to check ferror()
. Example below (not tested, but you get the idea).
main() {
int c;
do {
c = getchar();
if (c == EOF && ferror()) {
perror("getchar");
}
else {
putchar(c);
}
}
while(c != EOF);
}