{如何INT I = 999;焦C = I;} {从焦炭C = 999不同;}? [英] How is {int i=999; char c=i;} different from {char c=999;}?

查看:135
本文介绍了{如何INT I = 999;焦C = I;} {从焦炭C = 999不同;}?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的朋友说,他阅读了一些网页上,这样它们是不同的,但怎么可能,两个人可能有不同?

My friend says he read it on some page on SO that they are different,but how could the two be possibly different?

案例1

 int i=999;
 char c=i;

案例2

 char c=999;

在第一种情况下,我们正在初始化整数 I 999 ,然后初始化 ç I ,这实际上是 999 。在第二种情况下,我们初始化 C 直接与 999 .The截断和一旁的信息丢失,如何在地球上是这两种情况有什么不同?

In first case,we are initializing the integer i to 999,then initializing c with i,which is in fact 999.In the second case, we initialize c directly with 999.The truncation and loss of information aside, how on earth are these two cases different?

修改

下面是我说的链接

<一个href=\"http://stackoverflow.com/questions/10503434/why-no-overflow-warning-when-converting-int-to-char\">why INT转换为char 时,没有溢出警告

一位成员评论说,有 - 这不是一回事。首先是分配,二是初始化

One member commenting there says --It's not the same thing. The first is an assignment, the second is an initialization

所以,是不是比只是一个编译器优化的问题多很多?

So isn't it a lot more than only a question of optimization by the compiler?

推荐答案

它们具有相同的语义。

999 的类型为 INT

int i=999;
char c=i;

I 创建为类型的对象 INT 并用 INT 999 ,具有明显的语义。

i created as an object of type int and initialized with the int value 999, with the obvious semantics.

C 类型的对象字符,和值初始化,这恰好是 999 。该值从隐含 INT 转换为字符

c is created as an object of type char, and initialized with the value of i, which happens to be 999. That value is implicitly converted from int to char.

的符号性平原字符是实现定义的。

The signedness of plain char is implementation-defined.

如果纯字符是一个无符号类型,转换的结果是很好的界定。该值降低模 CHAR_MAX + 1 。对于8位字节( CHAR_BIT == 8 ), CHAR_MAX + 1 将是256,和一个典型的实现存储的值将是 999%,256 231

If plain char is an unsigned type, the result of the conversion is well defined. The value is reduced modulo CHAR_MAX+1. For a typical implementation with 8-bit bytes (CHAR_BIT==8), CHAR_MAX+1 will be 256, and the value stored will be 999 % 256, or 231.

如果纯字符是一个符号类型,而 999 超过 CHAR_MAX ,转换产生一个实现定义的结果(或者,从C99,提出了一个实现定义的信号,但我知道没有实现,这样做的)。的通常的,对于具有二进制补码系统 CHAR_BIT == 8 ,结果将是 -25

If plain char is a signed type, and 999 exceeds CHAR_MAX, the conversion yields an implementation-defined result (or, starting with C99, raises an implementation-defined signal, but I know of no implementations that do that). Typically, for a 2's-complement system with CHAR_BIT==8, the result will be -25.

char c=999;

C 的类型字符的对象创建的。它的初始值是 INT 999 转换为字符 - 由完全一样的规则,我上述

c is created as an object of type char. Its initial value is the int value 999 converted to char -- by exactly the same rules I described above.

如果 CHAR_MAX&GT; = 999 (出现这种情况只有在 CHAR_BIT ,位在一个字节数是至少10),则转换是微不足道的。没有与 CHAR_BIT 为DSP的C实现(数字信号处理器)设置为,例如,32这不是你可能在大多数系统上运行跨东西。

If CHAR_MAX >= 999 (which can happen only if CHAR_BIT, the number of bits in a byte, is at least 10), then the conversion is trivial. There are C implementations for DSPs (digital signal processors) with CHAR_BIT set to, for example, 32. It's not something you're likely to run across on most systems.

您可能会更容易获得在第二种情况下的警告,因为它是转换一个恒定的前pression;在第一种情况中,编译器可能无法保持 I 的预期值的轨道。但是足够聪明的编译器可以警告双方,和足够天真的(但仍符合全)编译器会发出警告也没有。

You may be more likely to get a warning in the second case, since it's converting a constant expression; in the first case, the compiler might not keep track of the expected value of i. But a sufficiently clever compiler could warn about both, and a sufficiently naive (but still fully conforming) compiler could warn about neither.

正如我前面所说,一个价值转换为符号的类型,当源值不适合目标类型的结果,是实现定义。我想这是可以想象的实现可以定义恒定和非恒定的前pressions不同的规则。这将是一个乖张的选择,虽然,我不知道甚至DS9K做到这一点。

As I said above, the result of converting a value to a signed type, when the source value doesn't fit in the target type, is implementation-defined. I suppose it's conceivable that an implementation could define different rules for constant and non-constant expressions. That would be a perverse choice, though; I'm not sure even the DS9K does that.

对于引用的评论第一个是赋值,第二个是一个初始化,这是不正确。两者都是初始化;有任一code段没有分配。存在这样一个差与一恒定值的初始化,而另一个不是。这意味着,顺便提一句,第二段可能出现在文件范围内,任何函数外,而第一却不能。

As for the referenced comment "The first is an assignment, the second is an initialization", that's incorrect. Both are initializations; there is no assignment in either code snippet. There is a difference in that one is an initialization with a constant value, and the other is not. Which implies, incidentally, that the second snippet could appear at file scope, outside any function, while the first could not.

这篇关于{如何INT I = 999;焦C = I;} {从焦炭C = 999不同;}?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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