SmartVessel

Foucs on C++

博客园 首页 新随笔 联系 订阅 管理
int arrary[]={23,34,12,17,204,99,16};
#define TOTAL_ELEMENTS (sizeof(array))/sizeof(arrary[0])
main()
{
int d=-1,x;
if(d<=TOTAL_ELEMENTS-2)
{
x
=arrary[d+1]
}
}

sizeof的返回类型是unsigned int。if语句中的,d由int类型被提升为unsigned int。结果-1变成正数255。if就不能被执行了。

总之,朝表示范围大的方向扩展:

First, if either operand is long double, the other is converted to long double.
Otherwise, if either operand is double, the other is converted to double.
Otherwise, if either operand is float, the other is converted to float.
Otherwise, the integral promotions are performed on both operands; then, if either operand is unsigned long int, the other is converted to unsigned long int.
Otherwise, if one operand is long int and the other is unsigned int, the effect depends on whether a long int can represent all values of an unsigned int; if so, the unsigned int operand is converted to long int; if not, both are converted to unsigned long int.
Otherwise, if one operand is long int, the other is converted to long int.
Otherwise, if either operand is unsigned int, the other is converted to unsigned int.
Otherwise, both operands have type int.
 

posted on 2011-04-24 21:14  SmartVessel  阅读(257)  评论(0编辑  收藏  举报