我在代碼審查過程中發現了一些令人困惑的代碼,並且有點困惑。做一些研究,我發現這種情況。我寫的這個代碼示例突出問題這個字符應該是無符號的嗎?
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