getchar() function reads a character from the keyboard (ie, stdin
)
In the condition inside the given while
loop, getchar()
is called before each iteration and the received value is assigned to the integer c
.
Now, it must be understood that in C, the standard input (stdin
) is like a file. ie, the input is buffered. Input will stay in the buffer till it is actually consumed.
stdin
is actually the standard input stream.
getchar()
returns the the next available value in the input buffer.
The program essentially displays whatever that was read from the keyboard; including white space like \n
(newline), space, etc.
ie, the input is the input that the user provides via the keyboard (stdin
usually means keyboard).
And the output is whatever we provide as input.
The input that we provide is read character by character & treated as characters even if we give them as numbers.
getchar()
will return EOF
only if the end of file is reached. The ‘file’ that we are concerned with here is the stdin
itself (standard input).
Imagine a file existing where the input that we provide via keyboard is being stored. That’s stdin
.
This ‘file’ is like an infinite file. So no EOF
.
If we provide more input than that getchar()
can handle at a time (before giving it as input by pressing enter), the extra values will still be stored in the input buffer unconsumed.
The getchar()
will read the first character from the input, store it in c and print
cwith
putchar(c)`.
During the next iteration of the while
loop, the extra characters given during the previous iteration which are still in stdin
are taken during while ((c = getchar()) != EOF)
with the c=getchar()
part.
Now the same process is repeated till there is nothing left in the input buffer.
This makes it look as if putchar()
is returning a string instead of a single character at a time if more than one character is given as input during an iteration.
Eg: if input was
abcdefghijkl
the output would’ve been the same
abcdefghijkl
If you don’t want this behaviour, you can add fflush(stdin); right after the putchar(c);
.
This will cause the loop to print only the first character in the input provided during each iteration.
Eg: if input was
adgbad
only a
will be printed.
The input is sent to stdin
only after you press enter.
putchar() is the opposite of getchar()
. It writes the output to the standard output stream (stdout
, usually the monitor).
EOF
is not a character present in the file. It’s something returned by the function as an error code.
You probably won’t be able to exit from the give while
loop normally though. The input buffer will emptied (for displaying to the output) as soon as something comes into it via keyboard and the stdin
won't give EOF
.
For manually exiting the loop, EOF
can be sent using keyboard by pressing
ctrl+D in Linux and
ctrl+Z in Windows
eg:
while ((c = getchar()) != EOF)
{
putchar(c);
fflush(stdin);
}
printf("\nGot past!");
If you press the key combination to give EOF
, the message Got past!
will be displayed before exiting the program.
If stdin
is not already empty, you will have to press this key combination twice. Once to clear this buffer and then to simuate EOF
.
EDIT: The extra pair of parenthesis around c = getchar()
in while ((c = getchar()) != EOF)
is to make sure that the value returned by getchar()
is first assigned to c
before that value is compared with EOF
.
If this extra parenthesis were not there, the expression would effectively have been while (c = (getchar() != EOF) )
which would've meant that c
could have either of 2 values: 1
(for true) or 0
(for false) which is obviously not what is intended.