Printing value of EOF

Mar 29, 2015 at 11:09pm
Can someone create me a small program to print the value of EOF (end of file).
I am doing the following. what's wrong here? also in Xcode the program run does not even stop even if I have return 0; statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>

int main(int argc, const char * argv[]) {
    char c;
    
    printf("Enter some text: ");
    
    c = getchar();
    while (c != EOF) {
        putchar(c);
        c = getchar();
    }
    
    printf("%c is end of file", EOF);
    
    return 0;
}
Mar 29, 2015 at 11:14pm
Mar 30, 2015 at 1:32am
The main issue is that EOF is not a char; it is an int.

A secondary issue is that most modern operating systems don't have an EOF character at the end of files -- at least, not one that you can access directly.

EOF is a C construct that only exists in your program. It's purpose is only to let you know that you have read all characters from file and no more are available. Since all character codes are possible in any file, the EOF value must have more bits than available in a single character.

Hope this helps.
Mar 30, 2015 at 3:12am
But I get the same thing with int c; as well. Do you think you can write a few lines of code to explain. Thanks.
Mar 30, 2015 at 10:06am
printf("%d ...
Topic archived. No new replies allowed.