tags:

views:

1130

answers:

6

Hoe do you get to see the last print, in other words what to put in for EOF, I checked the defines and it says EOF is -1.

And if you Ctrl-D you won't see nothing?

#include <stdio.h>

int main() {
 int c;
 while((c = getchar() != EOF)) {
  printf("%d\n", c);
 }
 printf("%d - at EOF\n", c);
}
+4  A: 

EOF means end of file. It's a sign that the end of a file is reached, and that there will be no data anymore.

Edit:

I stand corrected. In this case it's not an end of file. As mentioned, it is passed when CTRL+d (linux) or CTRL+z (windows) is passed.

Ikke
-1: it may mean an error occured.
Bastien Léonard
+4  A: 

The value of EOF is a negative integer to distinguish it from "char" values that are in the range 0 to 255. It is typically -1, but it could be any other negative number ... according to the POSIX specs, so you should not assume it is -1.

The ^D character is what you type at a console stream on UNIX/Linux to tell it to logically end an input stream. But in other contexts (like when you are reading from a file) it is just another data character. Either way, the ^D character (meaning end of input) never makes it to application code.

As @Bastien says, EOF is also returned if getchar() fails. Strictly speaking, you should call ferror or feof to see whether the EOF represents an error or an end of stream. But in most cases your application will do the same thing in either case.

Stephen C
So you can never make the EOF on win32 make it to the appcode and see the last print?
Chris_45
@Chris_45 - I'm talking about the meaning of EOF. The root cause of the bug in your "appcode" is something completely different -- see @Lucas's answer,
Stephen C
+4  A: 

On Linux systems and OS X, the character to input to cause an EOF is Ctrl-D. For Windows, it's Ctrl-Z.

Depending on the operating system, this character will only work if it's the first character on a line, i.e. the first character after an Enter. Since console input is often line-oriented, the system may also not recognize the EOF character until after you've followed it up with an Enter.

And yes, if that character is recognized as an EOF, then your program will never see the actual character. Instead, a C program will get a -1 from getchar().

Carl Smotricz
Ok but what's the difference between Ctrl-z and Ctrl-D on Windows?Ctrl-z = EFO Ctrl-D = kill?
Chris_45
how did you manage to print out the keys???? Wooooah
gotch4
@Chris_45: On Windows, Ctrl-Z marks EOF, Ctrl-D is just Ctrl-D (or character 04). @gotch4: It's standard (but little used) HTML: < kbd >.
Carl Smotricz
@Chris_45: Ctrl-D corresponds to ASCII EOT (end-of-transmission), MS-DOS however used Ctrl-Z (ASCII SUB) for compatibility with CP/M, and Windows inherits that. In CP/M the EOF character was actually a character in the file, because all files had to be multiples of 128 characters. The character used to signal EOF is specific to the OS not the programming language. http://en.wikipedia.org/wiki/End-of-file
Clifford
Well I meant the difference between Ctrl-Z and Ctrl-C? not Ctrl-d
Chris_45
Then say so! :) Ctrl-C will usually kill a program that's accepting input from the console. Ctrl-D will mark the end of input but the program could continue running after that.
Carl Smotricz
+10  A: 

You should change your parenthesis to

while((c = getchar()) != EOF)

Because the "=" operator has a lower precedence than the "!=" operator. Then you will get the expected results. Your expression is equal to

while (c = (getchar()!= EOF))

You are getting the two 1's as output, because you are making the comparison "c!=EOF". This will always become one for the character you entered and then the "\n" that follows by hitting return. Except for the last comparison where c really is EOF it will give you a 0.

EDIT about EOF: EOF is typically -1, but this is not guaranteed by the standard. The standard only defines about EOF in section 7.19.1:

EOF which expands to an integer constant expression, with type int and a negative value, that is returned by several functions to indicate end-of-file, that is, no more input from a stream;

It is reasonable to assume that EOF equals -1, but when using EOF you should not test against the specific value, but rather use the macro.

Lucas
We have a winner.
Pod
Great add about not always being -1.. most people overlook that
0A0D
+3  A: 

Couple of typos:

while((c = getchar())!= EOF)
in place of:
while((c = getchar() != EOF))
Also getchar() treats a return key as a valid input, so you need to buffer it too.EOF is a marker to indicate end of input. Generally it is an int with all bits set.

#include <stdio.h>
int main()
{
 int c;
 while((c = getchar())!= EOF)
 {
  if( getchar() == EOF )
    break;
  printf(" %d\n", c);
 }
  printf("%d %u %x- at EOF\n", c , c, c);
}
prints:
49
50
-1 4294967295 ffffffff- at EOF
for input:
1
2
<ctrl-d>
Neeraj
Isn't there a bug in your code as well? You call `getchar()` twice (once in the while loop and once in the if), so very second input will be lost...
Heinzi
Have you tested this working? You're calling getchar twice..
Pod
It works because the second getchar() gets the '\n' from hitting return.
Lucas
@Heinzi the second getchar() is used to capture the "\n" from return. This was done to explain the behavior OP got in hi code..
Neeraj
+1  A: 
#include <stdio.h>

int main() {
    int c;
    while((c = getchar()) != EOF) { //precedence of != is greater than =, so use braces
        printf("%d\n", c);
    }
    printf("%d - at EOF\n", c);
}

I think this is right way to check value of EOF. And I checked the output.

For INPUT: abc and Enter I got OUTPUT: 97 98 99 10. ( the ASCII values)

For INPUT Ctrl-D I got OUTPUT: -1 - at EOF. So I think -1 is the value for EOF.

Try other inputs instead of Ctrl-D, like Ctrl-Z. I think it varies from compiler to compiler.

srikanth rongali
Sure, `stdio.h` defines `EOF` as `-1`. But that shouldn't interest you, nor do you need to look at it or print it out. Just check for EOF in your code and let the compiler worry about the details.
Carl Smotricz