Exercise 1.6 - Verify the value of EOF

Question

Verify the expression getchar() !=EOF is 0 or 1.

Solution

/**
 * Exercise 1.6 - Verify that the expression getchar() != EOF is 0 or 1
 *
 **/

#include <stdio.h>

int main(int argc, char *argv[]) {
    int c;

    while ((c = getchar()) != EOF) {
        printf("%d ", c != EOF);
        putchar(c);
    }

    printf("\n%d\n", c != EOF);
}

Explanation

1. This program is similar to the previous one Ex 1.5, wherein after it gets the input, it prints the value of the expression getchar() != EOF.

  1. For a file with this contents

$cat afile
contents
  1. We compile and run the program.

$gcc Ex1.6_verifyeof.c -o eof
$ ./eof < afile
1 c1 o1 n1 t1 e1 n1 t1 s1

0

4. We see that when char is not EOF, it is printing 1 and when it is EOF, 0 is printed.