加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

isspace()是否接受getchar()值?

发布时间:2020-12-16 10:14:24 所属栏目:百科 来源:网络整理
导读:如果输入为 representable as unsigned char 或等于EOF,则 isspace() 有效. getchar() 从stdin读取下一个字符. 当getchar()!= EOF;所有getchar()返回的值都可以表示为unsigned char吗? uintmax_t count_space = 0;for (int c; (c = getchar()) != EOF; ) i
如果输入为 representable as unsigned char或等于EOF,则 isspace()有效.

getchar()从stdin读取下一个字符.

当getchar()!= EOF;所有getchar()返回的值都可以表示为unsigned char吗?

uintmax_t count_space = 0;
for (int c; (c = getchar()) != EOF; )
  if (isspace(c))
    ++count_space;

愿这段代码导致未定义的行为吗?

解决方法

根据C11 WG14 draft version N1570:

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

§7.21.7.5/2 The getc function is equivalent to fgetc

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

即,

> isspace()接受getchar()值
> all getchar()!= EOF值可表示为unsigned char
>这里没有未定义的行为.

如果你认为它太明显了(“它还能做什么”),那就再想一想.例如,在the related case:isspace(CHAR_MIN)可能是未定义的,即,将字符传递给字符分类函数可能是未定义的行为!

如果UCHAR_MAX> INT_MAX结果可能是实现定义的:

§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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读