0

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();
    }
}
Rorick
  • 8,857
  • 3
  • 32
  • 37
Chris_45
  • 8,769
  • 16
  • 62
  • 73

10 Answers10

11

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
  • 184,426
  • 49
  • 232
  • 263
5

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.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
3

you should use int and not char

Ron Klein
  • 9,178
  • 9
  • 55
  • 88
2

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
  • 3,993
  • 1
  • 29
  • 35
  • Ok, so you can't send a sequence of characters to represent EOF? – Chris_45 Sep 17 '09 at 07:58
  • @Chris_45: no, EOF in this sense is "out of band", i.e. not a valid character. – unwind Sep 17 '09 at 08:04
  • How do you best describe what it is, this "out of band"-thing? /Regards – Chris_45 Sep 17 '09 at 08:10
  • 1
    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 Sep 17 '09 at 14:27
  • 1
    for what it's worth, in windows C-Z is EOF (ie, it's end of input like C-D in linux). – d.j.yotta Jan 08 '15 at 01:16
0

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
  • 48,893
  • 5
  • 92
  • 171
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.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
0

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
  • 66,855
  • 13
  • 106
  • 140
0

It Still Works with char data type. But the tricks are checking the condition in the loop with int value.

First: let's check it. if you write the following code like

printf("%d",getchar());

And then if you give the input from the keyboard A You should see 65 which is ASCII value of the A or if you give CTRL-D then see -1.

So that if you implement this logic then the solving code is

#include<stdio.h>

int main()
{
    char c;
    while ((c = getchar()) != EOF){
        putchar(c);
        //printf("%c",c); // this is another way for output
    }
    return 0;
}
Ashfaqur_Rahman
  • 140
  • 1
  • 9
0

Windows: Ctrl+z

Unix: Ctrl+d

reference:EOF

yupe
  • 117
  • 1
  • 10
-1

hi i think it's becoz in a stream -1 is not one but two characters and the ascii for neither of them is -1 or whatever is used for EOF

baz
  • 1,317
  • 15
  • 10