tags:

views:

864

answers:

7

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();
    }
}
+3  A: 

you should use int and not char

Ron Klein
+7  A: 

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.

rahul
On Win32 platform it is also -1 (in stdio.h) : #define EOF (-1)
Manu
@Manu: That what was phoenix said, that it's a macro that evaluates to an int. The type of the literal -1 is int.
unwind
Converting int -1 to char, I think, it's also -1.
Cook Schelling
+4  A: 

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.

Charles Bailey
A: 

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

Pete Kirkham
+1  A: 

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;
}
Shirkrin
Ok, so you can't send a sequence of characters to represent EOF?
Chris_45
@Chris_45: no, EOF in this sense is "out of band", i.e. not a valid character.
unwind
How do you best describe what it is, this "out of band"-thing? /Regards
Chris_45
Ctrl-C / Ctrl-D / Ctrl-X etc. are used to enter special characters or send signals to your process. By pressing Ctrl-D for example you tell your program "There will be no further input, close stdin" which in turn will result in getchar() returning EOF.
Shirkrin
A: 

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.

Michael Burr
A: 

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.

mouviciel