为什么一定要短转换为C和C ++运算前一个int? [英] Why must a short be converted to an int before arithmetic operations in C and C++?

查看:143
本文介绍了为什么一定要短转换为C和C ++运算前一个int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我从这个问题,看来C ++继承了这一要求为<$ C $的转换C>短距离C.执行算术运算时到 INT 我可以挑选你的大脑,以 为什么在C介绍摆在首位?为什么不只是做这些操作为

From the answers I got from this question, it appears that C++ inherited this requirement for conversion of short into int when performing arithmetic operations from C. May I pick your brains as to why this was introduced in C in the first place? Why not just do these operations as short?

例如(从DYP的建议的意见采取的):

short s = 1, t = 2 ;
auto  x = s + t ;

X 将拥有的 INT 的类型。

推荐答案

如果我们看的理由国际标准的编程语言-C 6.3.1.8 常见的算术转换的它说(重点煤矿前进的):

If we look at the Rationale for International Standard—Programming Languages—C in section 6.3.1.8 Usual arithmetic conversions it says (emphasis mine going forward):

有关这些转换在标准的规则是轻微
  那些K&放大器的修改; R:修改适应加入
  类型和价值preserving规则。 明确许可加入
  在一个更广泛式进行计算不是绝对必要的,
  因为这有时会产生更小,更快code,不
  何况正确的答案往往
。计算,也可以
  由仿佛规则在一个窄类型进行,只要在同一
  得到最终的结果。明确的铸造总是可以被用于获得
  在所希望的类型的值

The rules in the Standard for these conversions are slight modifications of those in K&R: the modifications accommodate the added types and the value preserving rules. Explicit license was added to perform calculations in a "wider" type than absolutely necessary, since this can sometimes produce smaller and faster code, not to mention the correct answer more often. Calculations can also be performed in a "narrower" type by the as if rule so long as the same end result is obtained. Explicit casting can always be used to obtain a value in a desired type

6.3.1.8 从的 C99标准草案覆盖的常见的算术转换的这是适用于运算前pressions的操作数,例如部分 6.5.6 加法运算符的说道:

Section 6.3.1.8 from the draft C99 standard covers the Usual arithmetic conversions which is applied to operands of arithmetic expressions for example section 6.5.6 Additive operators says:

如果两个操作数的算术类型,在常见算术
  转换
对它们执行的。

If both operands have arithmetic type, the usual arithmetic conversions are performed on them.

我们发现在第类似的文本 6.5.5 乘法运算的为好。在的操作数的情况下,首先的整数促销的从应用部分 6.3.1.1 布尔,字符和整数的它说:

We find similar text in section 6.5.5 Multiplicative operators as well. In the case of a short operand, first the integer promotions are applied from section 6.3.1.1 Boolean, characters, and integers which says:

如果int可以重新present原始类型的所有值,该值
  转换为int类型;否则,将其转换为一个unsigned int。
  这些被称为整数促销活动 48)所有其它类型
  由整数提升不会改变。

If an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions.48) All other types are unchanged by the integer promotions.

这部分 6.3.1.1 的讨论的理由或整数促销国际标准的编程语言-C 其实更有意思,我会选择性地引用b / C太长充分报价:

The discussion from section 6.3.1.1 of the Rationale or International Standard—Programming Languages—C on integer promotions is actually more interesting, I am going to selectively quote b/c it is too long to fully quote:

实现掉进两大阵营可表征
  为符号preserving和价值preserving

[...]

无符号preserving方法呼吁促进两个较小
  无符号类型unsigned int类型。这是一个简单的规则,并产生
  类型,是独立的执行环境。

The unsigned preserving approach calls for promoting the two smaller unsigned types to unsigned int. This is a simple rule, and yields a type which is independent of execution environment.

价值preserving方法呼吁促进这些类型
  有符号整数,如果该类型可以正确地重新present的所有值
  原稿类型,并以其他方式促进这些类型为unsigned
  int类型。因此,如果执行环境重新presents短的东西
  比INT小,无符号短INT变;否则就成了
  unsigned int处理。

The value preserving approach calls for promoting those types to signed int if that type can properly represent all the values of the original type, and otherwise for promoting those types to unsigned int. Thus, if the execution environment represents short as something smaller than int, unsigned short becomes int; otherwise it becomes unsigned int.

这可以在某些情况下,一些意外的结果为无符号和更大的符号类型之间的隐式转换不一致的行为演示,有很多更像例子。虽然在大多数情况下,这导致了操作按预期工作

This can have some rather unexpected results in some cases as Inconsistent behaviour of implicit conversion between unsigned and bigger signed types demonstrates, there are plenty more examples like that. Although in most cases this results in the operations working as expected.

这篇关于为什么一定要短转换为C和C ++运算前一个int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆