首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关于整数晋升

关于整数晋升
EN

Stack Overflow用户
提问于 2012-04-26 15:26:34
回答 1查看 144关注 0票数 0

在研究C转换时,我发现了一段关于Integer晋升的文章,我还不太明白,它说:

"Integer types smaller than int are promoted when an operation is performed on them. If all values of the original type can be represented as an int, the value of the smaller type is converted to an int; otherwise, it is converted to an unsigned int"

考虑到:

  1. 任意无符号整数类型的秩等于相应的有符号整数类型的秩
  2. 长长int的秩大于长int的秩,后者大于int的秩,后者大于短int的秩,后者大于符号字符的秩。

问题是:为什么小于int的类型不应该用int表示?为什么一个无符号int可以表示一个int不应该表示的值?

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-26 15:32:21

虽然short的等级比int低,但它可以是相同的大小(以位为单位),例如16位系统上的sizeof(int) == sizeof(short) == 2。因此,unsigned short可能能够保存大于INT_MAX的值。

对于您问题的第二部分,这个部分的答案大致相同:unsigned int可以保存不能用int表示的值,即INT_MAX+1UINT_MAX

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10336543

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档