isspace()是否接受getchar()值? [英] Does isspace() accept getchar() values?

查看:91
本文介绍了isspace()是否接受getchar()值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

§7.21.7.6/2 getchar函数等效于getc,其参数为stdin.

§7.21.7.6/2 The getchar function is equivalent to getc with the argument stdin.

§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 ... unsigned char [...]中的文字是我的.

§7.21.7.1/2 [!=EOF case] ...the fgetc function obtains that character as an unsigned char converted to an int...text in [...] is mine.

  • isspace()接受getchar()
  • 所有getchar()!=EOF值都可以表示为unsigned char
  • 这里没有未定义的行为.
  • isspace() accepts getchar() values
  • all getchar()!=EOF values are representable as unsigned 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:

§6.3.1.3/3 否则,将对新类型进行签名并且无法在其中表示值;结果是实现定义的,还是引发实现定义的信号.

§6.3.1.3/3 Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.

这篇关于isspace()是否接受getchar()值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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