Does ctype.h still require unsigned char?

孤人 提交于 2019-12-03 18:03:18

问题


Traditionally, it was - strictly speaking - an error to pass a signed char to the ctype.h predicates because they were only defined for -1 to 255, so -128 to -2 could end up in reading outside array bounds.

Was this ever fixed, or do you still strictly speaking have to use unsigned char to avoid undefined behaviour in modern versions of C?


回答1:


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

Yes, from the C11 draft standard section 7.4 Character handling <ctype.h> paragraph 1 says (emphasis mine):

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.

This also holds for draft C99 standard as well, you can find it in the same section.



来源:https://stackoverflow.com/questions/17975913/does-ctype-h-still-require-unsigned-char

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!