signed和unsigned之二

先看下面一段程序,这段程序摘自《C 专家编程》:


#include <stdio.h>
int array[] = {23,34,12,17,204,99,16};
#define TOTAL_ELEMENTS (sizeof(array)/sizeof(array[0]))


int main(void)
{
    int d=-1,x;
    /*........*/


    if(d <= TOTALTOTAL_ELEMENTS - 2)
        x = array[d+1];
    /*........*/
    return 0;
}
如果是有这样一段程序的话,你永远无法知道x的值到底是多少,因为这句赋值语句x=array[d+1];根本不会执行。原因在哪?经过调试发现程序执行到if语句进行判断完之后,直接跳过下面一条语句的执行。下面来分析一下原因,因为sizeof求算类型大小时的返回值是unsigned int类型,而d是signed int,if语句测试两者大小,d会自动升级为unsigned int,-1转换为unsigned int是一个很大的正整数,所以表达式的值永远为假,因此后面的赋值语句永远不会执行。这里面就是类型转换带来的bug,如果稍不注意,则可能对整个工程或者项目造成无法预测的后果,而这个bug是很难直接调试能够调试得出来的。


表达式中的类型转换


类型转化包括强制类型转化和隐式转换,这里所谈的都是隐式转换。先来了解一下传统C(K&R C)中隐式类型转换的规则:


首先任何char、short int类型的操作数都会被转换成int类型,任何float类型都会被转换为double型。如果一个操作数是double型,那么另外一个操作数也被转换为double,计算结果也是double;如果一个操作数是long型,那么另外一个操作数也被转换为long型,计算结果也是long;如果一个操作数是unsigned,那么另外一个操作数也被转换为unsigned,计算结果为unsigned。


而在新的标准中却作了一些修改:


1. 整型升级:所有的char,short int和位段都会首先自动转换成int或者unsigned int。如果int能够表示源类型的所有值,那么就转换为int,否则转换为unsigned int。


2. 在计算表达式的值时,通常首先会将低类型(能够表示的数据范围小的数据类型)转换为高类型,再参与计算。但是这里要注意的一点是,如果表达式中存在float型,则不一定会被转换为double型,然后再进行计算。如有下面这段代码:


float f1,f2;
double d;
f1 = d*f2;
如果采用单精度进行计算的话,最后结果同双精度计算结果相同,那么对f2可能不进行转换。这一点和传统C是不同的,但是目前很少有编译器(VC就不支持)支持这点。


当表达式中存在无符号和有符号类型的操作数时,如果一个操作数是unsigned long int,那么另一个操作数也被转换为 unsigned long int;如果一个操作数是long int,另一个操作数是unsigned int。如果long int能够表达unsigned int的表示范围,则另一个操作数被转换为long int;否则两个操作数都被转换为unsigned long int;如果一个操作数是unsigned int,另一个操作数是int,那么另一个操作数被转换为unsigned int。


下面看一个例子:


假设int是16位,long int是32位。


那么对于-1L < 1U,因为-1L是signed long int型的,而1U是unsigned int型,由于signe long int能够完全表示unsigned int的范围,所以1U被转换为signed long int;


对于-1L>1UL,因为-1L是signed long int型的,而1UL是unsigned long int型,则-1L被转换为unsigned long int。


-----------------------------------------提升规则的英文版------------------------------------------------------------------

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.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值