我在代码审查过程中发现了一些令人困惑的代码,并且有点困惑。做一些研究,我发现这种情况。我写的这个代码示例突出问题这个字符应该是无符号的吗?
char d = '©';// this is -87,the copyright symbol , (actually its 169 unsigned)
if(ispunct(d)) // will assert.
{
}
所以,谁是bug修复程序员,做了以下内容:
char d = '©';// this is -87,the copyright symbol , (actually its 169 unsigned)
if(ispunct((unsigned char)d)) // will not assert, because it will be 169.
{
}
我的问题是,它是否是OK使char
无符号?理想情况下,我不会使用char
,而是使用Unicode字符来避免此类问题,但该软件非常旧,不会很快重新设计。
我正在使用Visual Studio 2008.可以在ctype.h
找到ispunct()
。
在同一个家族中的其他功能也是如此 - 例如'isalpha()','toupper()'等等。 – caf