整型字面常量(Integer Literals)的类型匹配

有下面两行代码:

for(int i=0; i<0xFFFFFFFF; ++i);

for(char ch=0; ch<0xFF; ++ch);

第一行代码不会陷入死循环,第二行代码则会死循环。

如果不明白,看了下面的内容,你应该就了解了:

 

整型常量的类型

The data type of an integer literal is determined by its form, value, and suffix.

The following table lists the integer literals and shows the possible data types. The smallest data type that can represent the constant value is used to store the constant.

Integer Literal

Possible Data Types

unsuffixed decimal

int, long int, unsigned long int, long long int

unsuffixed octal

int, unsigned int, long int, unsigned long int, long long int, unsigned long long int

unsuffixed hexadecimal

int, unsigned int, long int, unsigned long int, long long int, unsigned long long int

decimal, octal, or hexadecimal suffixed by u or U

unsigned int, unsigned long int, unsigned long long int

decimal suffixed by l or L

long int, long long int

octal or hexadecimal suffixed by l or L

long int, unsigned long int, long long int, unsigned long long int

decimal, octal, or hexadecimal suffixed by both u or U, and l or L

unsigned long intunsigned long long int    

decimal suffixed by ll or LL

long long int

octal or hexadecimal suffixed by ll or LL

long long int, unsigned long long int

decimal, octal, or hexadecimal suffixed by both u or U, and ll or LL

unsigned long long int

 

参考:http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7a.doc%2Flanguage%2Fref%2Fclrc02ic.htm

16 进制Integer literal 的类型是下面第一个能表示其值的类型:
int, long int, unsigned long int, long long int
10 进制Integer literal的类型是下面第一个能表示其值的类型:
int, unsigned int, long int, unsigned long int, long long int, unsigned long long int

整型变量或常量比较时的类型转换

int i = -1;
unsigned int j = 1;
if(i > j)
{
    //do something
}

上面代码中的i和j不是相同的类型,比较时会把非unsigned的i的类型转换为unsigned int.导致的结果就是something will be done.

 

原来第一个for循环中的0xFFFFFFFF 被识别成了unsigned int,比较时i被转换成unsigned int 来进行比较

而第二个for循环中的0xFF则在int的表示范围之内所以被识别成了int类型

 

以上只在VC6中测试通过,尽管鄙视吧

转载于:https://www.cnblogs.com/xueguangfeng/archive/2012/05/07/Integer_Literals_Type.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值