本文介绍了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 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() 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:

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

10-18 22:20