为什么(getchar()!= EOF)总是等于1 [英] why (getchar() != EOF) always equal to 1

查看:163
本文介绍了为什么(getchar()!= EOF)总是等于1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< stdio.h>


int main(){


printf("%d \ nn" ,getchar()!= EOF);


返回0;

}


当我运行此程序时,无论我把它作为输入,输出

总是等于1,从不0(零)或5


为什么?

解决方案



" arnuld" < ge ********* @ gmail.comwrote in message

news:11 ********************* *@v33g2000cwv.googlegr oups.com ...


#include< stdio.h>


int main( ){


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


返回0;

}


当我运行这个程序时,无论我把它作为输入,输出

总是等于1,从不0(零)或5



您是否提供了控制台定义为文件结尾的内容?


例如,Microsoft Windows使用ctrl-Z,UNIX使用ctrl-D,

等。


>

为什么?



您可能没有为您的

操作系统发送正确的击键。请查阅您的文档。


-Mike


arnuld写道:


#include< stdio.h>


int main(){


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


返回0;

}


当我运行这个程序时,无论我把它作为输入,输出

总是等于1,从不0(零)或5



如果你输入你系统上文件末尾的任何内容

(在Unix系统上的ctrl-D)它将是0.


布尔表达式只能是0或1.你从哪里得到5个?


-

Ian Collins。


< blockquote> 3月8日上午11:42,Ian Collins< ian-n ... @ hotmail.comwrote:


好​​吧,如果你键入无论你系统中的文件结尾是什么,都是

(Unix上的ctrl-D就像系统一样ms)它将是0.



ok。我使用Linux,EOF字符是一个整数-1。如果我输入-1

输出仍然是1

但是如果我输入Ctrl-D然后输出为0。 (零)。


布尔表达式只能是0或1.你从哪里得到5?



i实际上很困惑。


EOF = -1


我的系统(Arch Linux i686,gcc 4.1.2)


顺便说一句,如果EOF被定义为< stdio.h>内的整数,那为什么我

得到1当我给出-1时作为输入?


Ian Collins。



#include <stdio.h>

int main() {

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

return 0;
}

when i run this programme, no matter what i give it as input, output
is always equal to 1, never 0 (zero) or 5

why ?

解决方案


"arnuld" <ge*********@gmail.comwrote in message
news:11**********************@v33g2000cwv.googlegr oups.com...

#include <stdio.h>

int main() {

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

return 0;
}

when i run this programme, no matter what i give it as input, output
is always equal to 1, never 0 (zero) or 5

Did you supply whatever your console defines as end-of-file?

For example, Microsoft Windows uses ctrl-Z, UNIX uses ctrl-D,
etc.

>
why ?

You''ve probably not sent the proper keystroke(s) for your
operating system. Consult your documentation.

-Mike


arnuld wrote:

#include <stdio.h>

int main() {

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

return 0;
}

when i run this programme, no matter what i give it as input, output
is always equal to 1, never 0 (zero) or 5

Well if you type whatever the end of file character on you system is
(ctrl-D on Unix like systems) it will be 0.

A boolean expression can only be 0 or 1. Where did you get 5 from?

--
Ian Collins.


On Mar 8, 11:42 am, Ian Collins <ian-n...@hotmail.comwrote:

Well if you type whatever the end of file character on you system is
(ctrl-D on Unix like systems) it will be 0.


ok. i use Linux, EOF character is an integer "-1". if i enter "-1"
output is still "1"
but if i type "Ctrl-D" then output is "0" (zero).

A boolean expression can only be 0 or 1. Where did you get 5 from?

i was confused actually.

EOF = -1

on my system (Arch Linux i686, gcc 4.1.2)

BTW, if EOF is defined as an integer inside <stdio.h>, then why do i
get "1" when i give "-1" as input ?

Ian Collins.



这篇关于为什么(getchar()!= EOF)总是等于1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆