【发布时间】:2017-11-05 07:07:19
【问题描述】:
isspace() 在输入为representable as unsigned char 或等于EOF 时有效。
getchar() 从标准输入读取下一个字符。
当getchar()!=EOF;是否所有getchar() 返回值都可以表示为unsigned char?
uintmax_t count_space = 0;
for (int c; (c = getchar()) != EOF; )
if (isspace(c))
++count_space;
此代码可能会导致未定义的行为吗?
【问题讨论】:
-
所有 ctype.h 函数都经过设计,以便它们可以处理可能是字符或
EOF的输入。来自 C 的基本原理:“由于这些函数通常主要用作宏,因此它们的域仅限于可以用 unsigned char 表示的小的正整数,加上 EOF 的值。” -
@Lundin
isspace(CHAR_MIN)在某些平台上未定义 (follow the link in the question or in the answer below)。
标签: c language-lawyer c11