问题描述
isspace()
如果输入是可表示为unsigned char
或等于EOF
.
getchar()
从stdin读取下一个字符.
getchar()
reads the next character from stdin.
当getchar()!=EOF
时;所有getchar()
返回的值都可以表示为unsigned char
吗?
When getchar()!=EOF
; are all getchar()
returned values representable as unsigned char
?
uintmax_t count_space = 0;
for (int c; (c = getchar()) != EOF; )
if (isspace(c))
++count_space;
此代码是否可能导致未定义的行为?
May this code lead to the undefined behavior?
推荐答案
根据C11 WG14草案版本N1570 :
§7.21.7.5/2 getc
功能等效于fgetc
...
§7.21.7.5/2 The getc
function is equivalent to fgetc
...
§7.21.7.1/2 [!=EOF
情况] ... fgetc
函数获取该字符作为转换为int
...
§7.21.7.1/2 [!=EOF
case] ...the fgetc
function obtains that character as an unsigned char
converted to an int
...
即
-
isspace()
接受getchar()
值 - 所有
getchar()!=EOF
值都可以表示为unsigned char
- 这里没有未定义的行为.
isspace()
acceptsgetchar()
values- all
getchar()!=EOF
values are representable asunsigned char
- there is no undefined behavior here.
如果您认为它太明显了(还有什么其他意义"),请再考虑一下.例如,在相关情况中:isspace(CHAR_MIN)
可能是不确定的,即,将字符传递给字符分类功能!
If you think it is too obvious ("what else can it be"), think again. For example, in the related case: isspace(CHAR_MIN)
may be undefined i.e., it may be undefined behavior to pass a character to a character classification function!
如果UCHAR_MAX > INT_MAX
结果可能是实现定义的:
If UCHAR_MAX > INT_MAX
the result may be implementation-defined:
这篇关于isspace()是否接受getchar()值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!