传统上,严格来说,将 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
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/