Determining EOF expression

2019-09-04 05:44发布

问题:

I need to verify that the expression getchar() ! = EOF is 0 or 1. My current code:

#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);
}

When I try to run that I get

98980980
1 91 81 91 81 01 91 81 01 

I`m not sure if I got this right.

EDIT:

Ok the question was actually " How to generate EOF " and the solution was to press ctrl+D.

回答1:

I'm not very sure if you want this answer, but as per my understanding, what information you're looking for is

  • If you input any valid character, getchar() != EOF yields 1.
  • If you press CTRL+D (on linux), or CTRL+Z (on windows), it will generate EOF and getchar() != EOF yields 0.


标签: c eof getchar