c - ctype.h 是否仍然需要 unsigned char?

标签 c undefined-behavior c11 ctype

传统上,严格来说,将 signed char 传递给 ctype.h 谓词是错误的,因为它们仅为 -1 到 255 定义,所以-128 到 -2 可能最终读取到数组边界之外。

这是否已经修复,或者严格来说您仍然必须使用 unsigned char 来避免现代 C 版本中的未定义行为?

最佳答案

do you still strictly speaking have to use unsigned char to avoid undefined behavior in modern versions of C?

是的,来自 C11 draft standard 7.4 部分 Character handling 段落 1 说(强调我的):

The header declares several functions useful for classifying and mapping characters.198) In all cases the argument is an int, the value of which shall be representable as an unsigned char or shall equal the value of the macro EOF. If the argument has any other value, the behavior is undefined.

这也适用于 draft C99 standard同样,您可以在同一部分中找到它。

关于c - ctype.h 是否仍然需要 unsigned char?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17975913/

相关文章:

c++ - UB 递增 NULL 指针导致的错误示例

c - 为什么 "noreturn"函数返回?

c - 是否有任何 C 库为 GNU/Linux 实现 C11 线程?

c - gdb 完成后获取状态

c - 使用 qsort 对多个数组进行排序

c - 严格的别名规则是什么?

c - vsnprintf 和 vsprintf_s 有什么区别?

c - 循环遍历所有可能的长度为 n 的字符串

c - 如何调试我的 C 应用程序的 I/O 缓冲?

c++ - 这个 C++ 成员初始化行为是否定义良好?