整数转换(显性和隐性铸造) [英] Integer conversion (explicit and implicit casting)

查看:101
本文介绍了整数转换(显性和隐性铸造)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我环顾四周,才发现涉及指针更为复杂的职位。我正在学习C,并且只是想确认一下我的几个例子理解。 (这些例子分别假定为32位和16位整型和短整型大小。)

I looked around and only found more complicated posts involving pointers. I'm learning C, and just wanted to confirm my understanding of a few examples. (These examples assume int and short int sizes of 32 and 16 bits, respectively.)

初​​始code:

int int1 = 70000;
int int2;
short int shortInt1 = -70;
short int shortInt2, shortInt3;

和一些样本转换:

shortInt2 = int1 / shortInt1;

我的理解:结果
一世。司进行(70000 / -70),产生的结果-1000值
II。由于INT具有较高的precedence比短整型,结果被分配到一个匿名符号整数结果
三。匿名符号整数被转换为一个匿名签短整型结果
IV。匿名签订短整型值赋给shortInt2结果
诉匿名符号int和匿名签订短整型被垃圾回收结果
六。结果:-1000

My understanding:
i. Division is conducted (70000 / -70), yielding a value of -1000
ii. Because int has higher precedence than short int, the result is assigned to an anonymous signed int
iii. Anonymous signed int is cast to an anonymous signed short int
iv. The value of the anonymous signed short int is assigned to shortInt2
v. The anonymous signed int and anonymous signed short int are garbage collected
vi. Result: -1000

shortInt3 = (short) int1 / shortInt1;

我的理解:结果
一世。由于铸造拥有超过算术precedence,INT1被强制转换为匿名签短整型。溢出时,给它的4464结果值
II。该司进行,并考虑-63的结果分配给第二个匿名签短整型结果
三。第二个匿名签订短整型值赋给shortInt3结果
IV。匿名符号短整型被垃圾回收结果
诉结果:-63

My understanding:
i. Because casting has precedence over arithmetic, int1 is cast to an anonymous signed short int. Overflow occurs, giving it a value of 4464
ii. The division is conducted, and the result of -63 is assigned to a second anonymous signed short int
iii. The value of the second anonymous signed short int is assigned to shortInt3
iv. Both anonymous signed short ints are garbage collected
v. Result: -63

int2 = (short)(int1 / shortInt2);

这是由我最困惑的例子。据我所知,铸有超过算术precedence,但在这里,似乎周围的算术括号是给在铸造​​算术precedence。 (我想是有道理的,因为铸造操作需要一定的价值投。)

This is the example by which I'm most confused. I understand that casting has precedence over arithmetic, but here it seems the parentheses around the arithmetic are giving the arithmetic precedence over the casting. (Which I suppose makes sense, since the casting operation needs some value to cast.)

所以,我的理解:结果
一世。司进行,并为int具有较高的precedence,该师的价值被分配到一个匿名符号整型。结果
II。匿名符号整数强制转换为匿名签短整型结果
三。对匿名进行的算术右移签订短整型,将其扩大到另一个匿名的符号int结果
IV。第二个匿名符号int值分配给int2,则结果
诉匿名符号int(第一批),匿名签署short int与匿名的符号int(二)是垃圾回收结果
六。值:-1000

So, my understanding:
i. Division is carried out and, as int has higher precedence, the value of the division is assigned to an anonymous signed int.
ii. The anonymous signed int is cast to an anonymous signed short int
iii. Arithmetic right shift is performed on the anonymous signed short int, expanding it into another anonymous signed int
iv. The value of the second anonymous signed int is assigned to int2
v. The anonymous signed int (the first), anonymous signed short int, and anonymous signed int (the second) are garbage collected
vi. Value: -1000

推荐答案

您的问题使得一些不正确的假设和使用不正确的术语。我将通过什么地介绍一下C标准说,有关样品code开始。

Your question makes a number of incorrect assumptions and uses incorrect terminology. I'll start by explainin what the C standard says about your sample code.

您可以抓住C标准的最新草案的副本,的 N1570 。 通常的算术转换一节6.3.1.8描述;在整数促销的被描述6.3.1.1。

You can grab a copy of the latest draft of the C standard, N1570. The "usual arithmetic conversions" are described in section 6.3.1.8; the integer promotions are describe in 6.3.1.1.

int int1 = 70000;

由于你的假设, INT 为32位,所以这是大到足以容纳值 70000 。 (有系统,其中 INT 仅在16位;在这样的系统,这将给实现定义的结果。)

Given your assumptions, int is 32 bits, so it's big enough to hold the value 70000. (There are systems where int is only 16 bits; on such systems, this would give an implementation-defined result.)

int int2;
short int shortInt1 = -70;

70 是类型的常量 INT 。一元 - 运算符应用于所产生的价值,产生-70的 INT 值。初始化会导致 INT 值为转换的目标对象的类型。转换preserve值只要有可能,并且由于是保证至少有16位宽, shortInt1 是设置为明显的价值。

70 is a constant of type int. The unary - operator is applied to the resulting value, yielding an int value of -70. The initialization causes that int value to be converted to the type of the target object. Conversions preserve values whenever possible, and since short is guaranteed to be at least 16 bits wide, shortInt1 is set to the obvious value.

short int shortInt2, shortInt3;

shortInt2 = int1 / shortInt1;

除法运算符应用的通常的算术转换的到它的两个操作数。这是一个适度复杂的规则集,旨在保证两个操作数是相同类型的(在大多数情况下,C没有混合类型的算术运算符)。这起案件是相当简单:短整型将被转换为 INT / ,然后应用到两个 INT 操作数和产生一个 INT 的结果。该值是 -1000

The division operator applies the usual arithmetic conversions to its two operands. This is a moderately complicated set of rules, designed to guarantee that both operands are of the same type (for the most part, C doesn't have mixed-type arithmetic operators). This case is fairly simple: the short int operand is converted to int, and the / is then applied to two int operands and yields an int result. The value is -1000.

分配进而导致该结果从 INT 转换为。同样,足够大,以保存价值,所以 shortInt2 得到值 -1000 (类型为,而不是 INT )。

The assignment then causes that result to be converted from int to short. Again, short is big enough to hold that value, so shortInt2 gets the value -1000 (of type short, not int).

用C最前pressions都没有考虑到它们出现的背景下进行评估。在赋值的右手侧上的分裂不会受左侧的类型。它孤立评估的然后的如果有必要转换为目标类型。

Most expressions in C are evaluated without regard to the context in which they appear. A division on the right hand side of an assignment is not affected by the type of the left hand side. It's evaluated in isolation and then converted to the target type if necessary.

shortInt3 = (short) int1 / shortInt1;

演员转换的 INT1 键入,所以 / <值/ code>经营者有型的两个操作数。但是的通常的算术转换的整数运算包括的整数促销的,它都转​​换操作数键入 INT 。在 INT -by - INT 分裂产生一个 INT 结果,然后将其转化为并分配给 shortInt3

The cast converts the value of int1 to type short, so the / operator has two operands of type short. But the usual arithmetic conversions on integer operands include the integer promotions, which convert both short operands to type int. The int-by-int division yields an int result, which is then converted to short and assigned to shortInt3.

int2 = (short)(int1 / shortInt2);

该部门 INT1 / shortInt2 应用的通常的算术转换的,该转换右操作数从 INT INT 然后转换被转换为 INT -by结果C $ C>的演员。那么结果转换为 INT ,因为它被分配到一个 INT 目标对象。

The division int1 / shortInt2 applies the usual arithmetic conversions, which convert the right operand from short to int. The result of the int-by-int conversion is then converted to short by the cast. The short result is then converted to int because it's being assigned to an int target object.

现在清理你写了几件事情:

Now to clear up a few things you wrote:

由于INT具有较高的precedence比短整型,结果被分配到一个匿名的符号int

Because int has higher precedence than short int, the result is assigned to an anonymous signed int

类型没有precedence。运营商做的。 (类型拥有的级别的,它是用来确定*通常的算术转换。)

Types don't have precedence. Operators do. (Types have rank, which is used to determine the *usual arithmetic conversions".)

您似乎假设评估一个前pression的结果必须分配给某个对象(变量)。这是情况并非如此。评估一个前pression产生了的结果的。该结果是一个特定的值,并且是某些特定类型的,但它不需要被存储在任何位置。 (也许它会暂时保存在寄存器中,甚至在某些内存位置,但是这是一个实现细节,我们可以放心地忽略。)没有必要去创造一个无名氏任何持有一个前$ P $的结果pssion。结果根本的的。最终它可能存储在一个对象,或传递给函数,或作为另一运营商的操作数。这样做了的方式不是由C标准定义。

You seem to be assuming that the result of evaluating an expression must be assigned to some object (variable). That's not the case. Evaluating an expression yields a result. That result is a particular value, and is of some particular type, but it needn't be stored anywhere. (Perhaps it will be stored temporarily in a register, or even in some memory location, but that's an implementation detail that we can safely ignore.) There's no need to invent an "anonymous" anything to hold the result of an expression. The result simply is. Eventually it might be stored in an object, or passed to a function, or used as the operand of another operator. The way that's done is not defined by the C standard.

这篇关于整数转换(显性和隐性铸造)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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