看板Programming
标 题Re: 请问有没有compiler option 可以用来忽略sign ?
发信站交大资科_BBS (Mon Jun 19 17:08:37 2006)
转信站ptt!ctu-reader!ctu-peer!news.nctu!news.cis.nctu!cis_nctu
我找到解法,
/J (Default char Type is unsigned)
======================================
This option changes the default char type from signed char to unsigned char,
and the char type is zero-extended when widened to an int type. If a char value is
explicitly declared signed, the /J option does not affect it, and the value is
sign-extended when widened to an int type.
The /J option defines _CHAR_UNSIGNED, which is used with #ifndef in the LIMITS.H file
to define the range of the default char type.
Neither ANSI C nor C++ requires a specific implementation of the char type.
This option is useful when you are working with character data that will eventually be
translated into a language other than English.
==> 在 [email protected] (小心课业闭锁空间...) 的文章中提到:
> ※ 引述《[email protected] (小呆)》之铭言:
> : 请教各位,
> : 在VC++里, 以下程式的第 5 行并不会成立, 原因是 ch
> : 已经被转为负值,
> : 但在某些老旧的compiler里, 第5行会成立
> : 因为它直接做记忆体内容比对.
> : 请问VC++里有没有compiler option可以忽略 sign ?
> : 感谢
> : 1 void main() {
> : 2 char ch, x=0;
> : 3
> : 4 ch=0x81;
> : 5 if(ch == 0x81) x=1;
> if(ch == (char)0x81) x=1;
> : 6 }
> 这样试试
> 因为如果我没记错的话 单纯写数字在VC++里是视为int的
> 然後大范围资料和小范围资料是会把小的upcast到大的去比
> 於是它把char的0x81(-127)给upcast成int的0xFFFFFF81(也是-127) 再去比
> 把那个0x81强制变成char应该就可以成立了
--
* Origin: ★ 交通大学资讯科学系 BBS ★ <bbs.cis.nctu.edu.tw: 140.113.23.3>