getchar()和EOF混淆 [英] getchar() and EOF confusion
问题描述
大多数情况下,当我想从stdin获取输入时,我使用getchar()但是我从手册页本身得到了这个
:
"如果getchar()返回的整数值存储在一个变量
类型的char中,然后与整数常量EOF进行比较,
比较可能永远不会成功,因为符号扩展变量类型的变量
char加宽到整数是实现定义的
while(EOF!=(ch = getchar()))....
我就这样使用它。我可以遇到问题吗?
-
www.lispmachine.wordpress.com
我的电子邮件是@上面的博客。
Mostly when I want to take input from stdin I use getchar() but I get this
from man page itself:
"If the integer value returned by getchar() is stored into a variable of
type char and then compared against the integer constant EOF, the
comparison may never succeed, because sign-extension of a variable of type
char on widening to integer is implementation-defined"
while( EOF != (ch = getchar()) ) ....
I use it like that. Can I run into problems with that ?
--
www.lispmachine.wordpress.com
my email is @ the above blog.
推荐答案
arnuld说:
arnuld said:
大多数情况下,当我想从stdin获取输入时我使用getchar()但是我得到了
来自手册页本身:
"如果getchar()返回的整数值存储在变量
of
类型char然后与整数常量EOF进行比较,
比较可能永远不会成功,因为
类型的变量的符号扩展加宽to integer是实现定义的'
while(EOF!=(ch = getchar()))....
我用就是这样。我可以遇到问题吗?
Mostly when I want to take input from stdin I use getchar() but I get
this from man page itself:
"If the integer value returned by getchar() is stored into a variable
of
type char and then compared against the integer constant EOF, the
comparison may never succeed, because sign-extension of a variable of
type char on widening to integer is implementation-defined"
while( EOF != (ch = getchar()) ) ....
I use it like that. Can I run into problems with that ?
你可以遇到任何问题 - 但上面是一个很好的习语。
确保ch的类型是int。
-
Richard Heathfield< http://www.cpax.org.uk>
电子邮件:-http://万维网。 + rjh @
谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>
Usenet是一个奇怪的放置" - dmr 1999年7月29日
You can run into problems with anything - but the above is a good idiom.
Ensure that ch has type int.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
2008年10月15日星期三,Richard Heathfield写道:
On Wed, 15 Oct 2008, Richard Heathfield wrote:
你可以跑任何事情的问题 - 但上面是一个很好的习语。
确保ch的类型为int。
You can run into problems with anything - but the above is a good idiom.
Ensure that ch has type int.
哦......这就是K& R2使用int ch的原因。 ,谢谢:)
Oh.. thats why K&R2 uses "int ch" , thanks :)
2008年10月15日星期三09:47:14 +0500,arnuld< su ***** @ invalid.address>
写道:
On Wed, 15 Oct 2008 09:47:14 +0500, arnuld <su*****@invalid.address>
wrote:
>大多数情况下当我想从stdin获取输入时我使用getchar()但是我从手册页得到了这个
本身:
"如果getchar()返回的整数值存储到一个变量
类型的char中,然后与整数常量EOF进行比较,
比较可能永远不会成功,因为在扩展为整数时类型为
char的变量的符号扩展是实现定义的
while(EOF!=(ch = getchar()))....
我就这样使用它。我可以遇到问题吗?
>Mostly when I want to take input from stdin I use getchar() but I get this
from man page itself:
"If the integer value returned by getchar() is stored into a variable of
type char and then compared against the integer constant EOF, the
comparison may never succeed, because sign-extension of a variable of type
char on widening to integer is implementation-defined"
while( EOF != (ch = getchar()) ) ....
I use it like that. Can I run into problems with that ?
getchar将从流中获取的数据视为未签名。 EOF
保证为负数。你能看到这导致的地方吗?
-
删除del电子邮件
getchar treats the data it obtains from the stream as unsigned. EOF
is guaranteed to be negative. Can you see where this leads?
--
Remove del for email
这篇关于getchar()和EOF混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!