gpt4 book ai didi

c - 从 double 到 int 的隐式类型转换?

转载 作者:行者123 更新时间:2023-12-03 22:55:20 24 4
gpt4 key购买 nike

// Assuming these definitions
int x;
float y;
这有什么区别:
  x = y = 7.5;
和这个:
  y = x = 7.5;
为什么第一个打印 y值为 7.5 ,
和第二个打印 y7.00 ?

最佳答案

解释很简单:=从右到左关联,这意味着 x = y = 7.5;被评估为 x = (y = 7.5);因此本质上与:

y = 7.5;   // value is converted from double to float, y receives 7.5F
x = y; // value of y is converted from float to int, x receives 7 (truncated toward 0)

y = x = 7.5;被评估为 y = (x = 7.5); :
x = 7.5;   // 7.5 is converted to int, x receives value 7 (truncated toward 0)
y = x; // value of x is converted to float, y receives 7.0F

这些隐式转换可能违反直觉。您可能希望提高警告级别,让编译器警告您潜在的错误和不需要的副作用。

关于c - 从 double 到 int 的隐式类型转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55663477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com