Determining EOF expression

馋奶兔 提交于 2019-12-12 05:28:54

问题


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.


来源:https://stackoverflow.com/questions/29344880/determining-eof-expression

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!