为什么C/C ++会自动将char/wchar_t/short/bool/enum类型转换为int类型? [英] Why does C/C++ automatically convert char/wchar_t/short/bool/enum types to int?

查看:191
本文介绍了为什么C/C ++会自动将char/wchar_t/short/bool/enum类型转换为int类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,据我所知,整数提升提供:•char, wchar_t, bool, enum, short类型始终转换为int(或unsigned int).然后,如果表达式中的类型不同,将应用进一步的转换.

So, if I understood it well, integral promotion provides that: char, wchar_t, bool, enum, short types ALWAYS are converted to int (or unsigned int). Then, if there are different types in an expression, further conversion will be applied.

我了解得很好吗?

如果是,那么我的问题是:为什么好呢?为什么?不要成为char/wchar_t/bool/enum/short不必要的吗?我的意思是例如:

And if yes, then my question: Why is it good? Why? Don't become char/wchar_t/bool/enum/short unnecessary? I mean for example:

char c1;
char c2;
c1 = c2;

如前所述,char始终会转换为int,因此在这种情况下,自动转换后,它看起来像这样:

As I described before, char ALWAYS is converted to int, so in this case after automatic converting this looks like this:

int c1;
int c2;
c1 = c2;

但是,如果我知道char类型足以满足我的需求,那么我不明白为什么这样做会很好.

But I can't understand why is this good, if I know that char type will be enough for my needs.

推荐答案

您要查询的转化是通常的算术转化整数促销,定义于最新的ISO C标准的第6.3.1.8节.它们适用于大多数二进制运算符的操作数("binary"表示它们采用两个操作数,例如+*等). (规则对于C ++来说是相似的.在这个答案中,我将仅参考C标准.)

The conversions you're asking about are the usual arithmetic conversions and the integer promotions, defined in section 6.3.1.8 of the latest ISO C standard. They're applied to the operands of most binary operators ("binary" meaning that they take two operands, such as +, *, etc.). (The rules are similar for C++. In this answer, I'll just refer to the C standard.)

简而言之,通常的算术转换是:

  • 如果其中一个操作数为long double,则另一个操作数将转换为long double.
  • 否则,如果其中一个操作数为double,则另一个操作数将转换为double.
  • 否则,如果其中一个操作数为float,则另一个操作数将转换为float.
  • 否则,对两个操作数执行整数提升,然后应用一些其他规则将两个操作数归为同一类型.
  • If either operand is long double, the other operand is converted to long double.
  • Otherwise, if either operand is double, the other operand is converted to double.
  • Otherwise, if either operand is float, the other operand is converted to float.
  • Otherwise, the integer promotions are performed on both operands, and then some other rules are applied to bring the two operands to a common type.

整数促销在C标准的6.3.1.1节中定义.对于小于int的类型,如果类型int可以容纳该类型的所有值,则该类型的表达式将转换为int;否则,该类型的表达式将转换为int.否则将转换为unsigned int. (请注意,这意味着类型unsigned short的表达式可以转换为intunsigned int,具体取决于类型的相对范围.)

The integer promotions are defined in section 6.3.1.1 of the C standard. For a type narrower than int, if the type int can hold all the values of the type, then an expression of that type is converted to int; otherwise it's converted to unsigned int. (Note that this means that an expression of type unsigned short may be converted either to int or to unsigned int, depending on the relative ranges of the types.)

当声明未指定参数类型时,整数提升也将应用于函数参数.例如:

The integer promotions are also applied to function arguments when the declaration doesn't specify the type of the parameter. For example:

short s = 2;
printf("%d\n", s);

short的值提升为int.对于非可变函数不会发生这种提升.

promotes the short value to int. This promotion does not occur for non-variadic functions.

为什么要这样做的快速答案是该标准是这样说的.

The quick answer for why this is done is that the standard says so.

所有这些复杂性的根本原因是允许大多数CPU上可用的一组有限的算术运算.使用此规则集,仅需要所有算术运算符(移位运算符除外,这是一种特殊情况),才能对相同类型的操作数进行运算.没有short + long加法运算符;而是将short操作数隐式转换为long.对于小于int的类型,没有算术运算符;如果您添加两个short值,则两个参数都将提升为int,从而产生一个int结果(然后可能会转换回short).

The underlying reason for all this complexity is to allow for the restricted set of arithmetic operations available on most CPUs. With this set of rules, all arithmetic operators (other than the shift operators, which are a special case) are only required to work on operands of the same type. There is no short + long addition operator; instead, the short operand is implicitly converted to long. And there are no arithmetic operators for types narrower than int; if you add two short values, both arguments are promoted to int, yielding an int result (which might then be converted back to short).

某些CPU可以对窄操作数执行算术运算,但并非所有人都可以执行.如果没有这种统一的规则集, 编译器将不得不在不直接支持它的CPU上模拟狭窄的算术,而算术表达式的行为会根据不同而有所不同.目标CPU支持的操作.当前的规则是在跨平台的一致性和充分利用CPU操作之间的良好折衷.

Some CPUs can perform arithmetic on narrow operands, but not all can do so. Without this uniform set of rules, either compilers would have to emulate narrow arithmetic on CPUs that don't support it directly, or the behavior of arithmetic expressions would vary depending on what operations the target CPU supports. The current rules are a good compromise between consistency across platforms and making good use of CPU operations.

这篇关于为什么C/C ++会自动将char/wchar_t/short/bool/enum类型转换为int类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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