本文介绍了ctype.h 还需要无符号字符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

推荐答案

是的,来自 C11 草案标准 section 7.4 字符处理 <ctype.h> 段落 1 说(强调我的):

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

头部声明了几个对分类和映射有用的函数characters. 在所有情况下,参数都是一个 int,其值 应为可表示为无符号字符或应等于宏 EOF 的值.如果参数具有任何其他值,行为未定义.

这也适用于 C99 标准草案 同样,您可以在同一部分找到它.

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

这篇关于ctype.h 还需要无符号字符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 00:30