EOF 总是负数吗?
EOF
总是负数吗?
我正在考虑编写一个函数,该函数读取输入中的下一个单词,并返回在其中找到该单词的行号,如果已到达输入末尾,则返回 EOF 。如果 EOF 不一定为负,则该函数将不正确。
Is EOF
always negative?
I'm thinking of writing a function that reads the next word in the input and returns the line number the word was found in or EOF
if the end of the input has been reached. If EOF
is not necessarily negative, the function would be incorrect.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
EOF
始终为== EOF
。不要假设任何其他事情。在第二次阅读标准时(以及根据这里的其他一些评论),似乎
EOF
始终为负 - 并且对于此问题中指定的用途(行号或EOF
)它会起作用。我想要警告(并且仍然这样做)的是假设字符为正,而 EOF 为负。请记住,符合标准的 C 实现可能具有负字符值 - 这甚至在“C 编程语言”(K&R) 中提到过。打印字符始终为正,但在某些架构(可能都是古代)上,控制字符为负。 C 标准没有指定
char
类型是有符号还是无符号,唯一保证跨平台具有相同值的字符常量是'\0'
。EOF
is always== EOF
. Don't assume anything else.On a second reading of the standard (and as per some other comments here) it seems
EOF
is always negative - and for the use specified in this question (line number orEOF
) it would work. What I meant to warn against (and still do) is assuming characters are positive andEOF
is negative.Remember that it's possible for a standard conforming C implementation to have negative character values - this is even mentioned in 'The C programming language' (K&R). Printing characters are always positive, but on some architectures (probably all ancient), control characters are negative. The C standard does not specify whether the
char
type is signed or unsigned, and the only character constant guaranteed to have the same value across platforms, is'\0'
.是的,EOF 总是负数。
标准说:
请注意,对“plain”
char
进行签名没有问题。处理char
的
函数,专门将字符转换为unsigned char
,然后转换为int< /code>,以便所有有效字符都具有正值。例如:
Yes, EOF is always negative.
The Standard says:
Note that there's no problem with "plain"
char
being signed. The<stdio.h>
functions which deal withchar
s, specifically cast the characters tounsigned char
and then toint
, so that all valid characters have a positive value. For example:让该函数返回
问题已解决,无需依赖任何 EOF 值。调用者可以轻松测试是否大于或等于 0 是否成功调用,否则假设 EOF/IO 错误。
Have that function return
Problem solved, without a need for relying on any EOF values. The caller can easily test for greater-or-equal-to-zero for a successful call, and assume EOF/IO-error otherwise.
来自在线草案 n1256,17.9。 1.3:
EOF 始终为负数,但可能并不总是 -1。
对于此类问题,我更喜欢通过返回错误代码(
SUCCESS
、END_OF_FILE
、READ_ERROR
等)作为函数的返回值,然后将感兴趣的数据写入单独的参数,例如From the online draft n1256, 17.9.1.3:
EOF is always negative, though it may not always be -1.
For issues like this, I prefer separating error conditions from data by returning an error code (
SUCCESS
,END_OF_FILE
,READ_ERROR
, etc.) as the function's return value, and then writing the data of interest to separate parameters, such asEOF 是一个条件,而不是一个值。该标记的确切值是实现定义的。在很多情况下,它是一个负数。
EOF is a condition, rather than a value. The exact value of this sentinel is implementation defined. In a lot of cases, it is a negative number.
来自维基百科:
但没有参考...
来自安全编码:检测并处理输入输出错误
EOF 为负数,但仅当 sizeof(int) > sizeof(char)。
From wikipedia :
But no references ...
From Secure Coding : Detect and handle input and output errors
EOF is negative but only when sizeof(int) > sizeof(char).