What do you put in to end the program, -1, doesn't work:
#include <stdio.h>
//copy input to output
main() {
char c;
c = getchar();
while(c != EOF) {
putchar(c);
c = getchar();
}
}
What do you put in to end the program, -1, doesn't work:
#include <stdio.h>
//copy input to output
main() {
char c;
c = getchar();
while(c != EOF) {
putchar(c);
c = getchar();
}
}
Macro: int EOF
This macro is an integer value that is returned by a number of functions to indicate an end-of-file condition, or some other error situation. With the GNU library, EOF is -1. In other libraries, its value may be some other negative number.
The documentation for getchar
is that it returns the next character available, cast to an unsigned char
and then returned in an int
return value.
The reason for this, is to make sure that all valid characters are returned as positive values and won't ever compare as equal to EOF
, a macro which evaluates to a negative integer value.
If you put the return value of getchar
into a char
, then depending on whether your implementation's char
is signed or unsigned you may get spurious detection of EOF
, or you may never detect EOF
even when you should.
Signaling EOF
to the C library typically happens automatically when redirecting the input of a program from a file or a piped process. To do it interactively depends on your terminal and shell, but typically on unix it's achieved with Ctrl-D and on windows Ctrl-Z on a line by itself.
If the integer value returned by getchar() is stored into a variable of type char and then compared against the integer constant EOF, the comparison may never succeed, because sign-extension of a variable of type char on widening to integer is implementation-defined. -- opengroup POSIX standard
Hi,
I agree with all other people in this thread by saying use int c not char.
To end the loop (at least on *nix like systems) you would press Ctrl-D to send EOF.
In addition, if you like to get your characters echoed instantly rewrite your code like this:
#include<stdio.h>
int
main(void)
{
int c;
c = getchar();
while (c != EOF)
{
putchar(c);
c = getchar();
fflush(stdout); /* optional, instant feedback */
}
return 0;
}
If char is unsigned
by default for your compiler (or by whatever options are being used to invoke the compiler), it's likely that
(c == EOF)
can never be true. If sizeof(unsigned char) < sizeof( int)
, which is pretty much always true, then the promotion of the char
to an int will never result in a negative value, and EOF
must be a negative value.
That's one reason why all (or at least many if not all) the functions in the C standard that deal with or return characters specify int
as the parameter or return type.
EOF
is not an actual character or a sequence of characters. EOF
denotes the end of the input file or stream, i.e., the situation when getchar()
tries to read a character beyond the last one.
On Unix, you can close an interactive input stream by typing CTRL-D. That situation causes getchar()
to return EOF
. But if a file contains a character whose ASCII code is 4 (i.e., CTRL-D), getchar()
will return 4, not EOF
.