本文介绍了隐式类型转换:如果一个操作数是短&安培;另一种是字符,字符将被转换为短?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

K&安培; R指出,如果一个操作数为 INT 其它操作数将被转换为 INT 。当然,这是只有在所有其他规则(如长双浮动 unsigned int类型等)进行了跟踪。

K&R states, that if either operand is an int the other operand will be converted to int. Of course, that is only after all the other rules (like long double, float, unsigned int etc.) have been followed.

通过这一逻辑,字符将被转换为 INT ,如果另一个操作数是一个 INT 。但是,如果在操作的最大整数类型是

By that logic, char would be converted to int, if the other operand was an int. But what if the highest integer type in an operation is a short?

现在,显然我并不需要显式转换字符来一个更大的整数,但我不怀疑,不ANSI-C手柄之间的隐式转换字符短? K&安培; R不说任何东西。

Now, obviously I don't need to explicitly convert a char to a bigger integer, but I do wonder, does ANSI-C handle implicit conversion between char and short under the hood? K&R does not say anything about that.

我说,我有code以下行:

Say, I have the following lines of code:

char x = 'x';
short y = 42;
short z = x + y;

威尔 X 转换为?还是会有无中转开始呢?

Will x be converted to short? Or will there be no conversion to begin with at all?

只是为了说清楚:我并不要求是否或如何从字符转换为 。我只是想知道关于隐式类型转换会发生什么。

Just to make it clear: I'm not asking for whether or how to convert from char to short. I just want to know what happens in regards to implicit type conversions.

推荐答案

在整数促销将加入前两者转换为 INT

The "integer promotion" will convert both of them to int before the addition:

以下可在离pression用于任何一个int或unsigned int可
  使用

- 一个对象或前pression一个整型的整数转换等级少
  比int和unsigned int型的军衔。

— An object or expression with an integer type whose integer conversion rank is less than the rank of int and unsigned int.

...]
  如果int可以重新present原始类型的所有值,该值被转换为int;
  否则,将其转换为一个unsigned int。这些被称为整数
  促销活动。

[...] 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.

(ISO / IEC ISO / IEC 9899:1999(E),§6.3.1.1)

(ISO/IEC ISO/IEC 9899:1999 (E), §6.3.1.1)

这篇关于隐式类型转换:如果一个操作数是短&安培;另一种是字符,字符将被转换为短?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 15:30