Chinaunix首页 | 论坛 | 博客
  • 博客访问: 8016012
  • 博文数量: 159
  • 博客积分: 10424
  • 博客等级: 少将
  • 技术积分: 14615
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-14 12:45
个人简介

啦啦啦~~~

文章分类
文章存档

2015年(5)

2014年(1)

2013年(5)

2012年(10)

2011年(116)

2010年(22)

分类: C/C++

2011-10-01 18:22:24

本文的copyleft归gfree.wind@gmail.com所有,使用GPL发布,可以自由拷贝,转载。但转载请保持文档的完整性,注明原作者及原链接,严禁用于任何商业用途。
作者:gfree.wind@gmail.com
博客:linuxfocus.blog.chinaunix.net


Usual Arithmetic Conversion: The integer promotions are performed on both operands. Then the following rules are applied to the promoted operands:
If both operands have the same type, then no further conversion is needed.
Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted the type of the operand with unsigned integer type.
Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.
Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.

献丑翻译成中文:
整数提升:整数提升可以应用于所有的操作数,下面是整数提升的规则:
如果两个操作数具有相同的类型,那么不需要任何的转换;
不然,如果两个操作数同为有符号整数或者同为无符号整数,则rank小的操作数需要被提升为rank大的操作数的类型;
不然,一个无符号操作数的rank大于或者等于另外一个有符号操作数,则该有符号操作数的类型会提升为无符号操作数的类型;
不然,一个有符号操作数可以表示另一个无符号数的所有的值,那么那个无符号操作数的类型需转换为该有符号类型;
不然,两个操作数都需要转换为有符号数对应的无符号类型——这种情况,我没有找出实例,哪位朋友指点一下呢。

虽然整数类型提升为C的基础概念,但是不看标准的话,我想大多数人不会特别清楚——包括我在内,即使平时因为注意这个问题,而不会因此犯错。尤其是第4条,至少我以前就不甚了解。我会认为有符号数都会转为无符号数的,不会认为无符号数也会转为有符号数的。

为此,我又写了一篇博文http://blog.chinaunix.net/space.php?uid=23629988&do=blog&id=2938698专门针对具体的整数提升情况

更新:
此文还有理解不深的地方,另也一篇博文更新:http://blog.chinaunix.net/space.php?uid=23629988&do=blog&id=2950299
阅读(7286) | 评论(12) | 转发(1) |
给主人留下些什么吧!~~

GFree_Wind2011-10-03 15:03:20

Rainyzzj: 曾经面试考到过。嘿嘿,还好C++ primer里头说过。.....
C++primer应该不会说得这么详细吧。。。

Rainyzzj2011-10-02 21:37:49

曾经面试考到过。嘿嘿,还好C++ primer里头说过。