[英]typecasting long to int and short in C
long x = <some value>
int y = <some value>
I want to subtract y from x , which of the following will give me different or same results我想从 x 中减去 y ,以下哪个会给我不同或相同的结果
x = (int)x - y;
x = x-y
x = short(x) - short(y)
The type long
is "bigger" than int
. long
类型比int
“大”。 Smaller types can be automatically casted to bigger types.较小的类型可以自动转换为较大的类型。 So, this code:所以,这段代码:
someLongNumber + someIntNumber
is basically equal to基本上等于
someLongNumber + (long)someIntNumber
So the output will be long
.所以输出会很long
。 You don't have to cast if you want to place the result in x
.如果要将结果放在x
则不必进行强制转换。 However, if you want to place it in y
, you must cast the x
operand to int
( (int)x
), or the whole operation ( (int)(x - y)
).但是,如果要将它放在y
,则必须将x
操作数转换为int
( (int)x
) 或整个操作 ( (int)(x - y)
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.