为什么 printf 隐式 float 到 int 转换不起作用?
请帮助我理解以下 C 输出:
#include<stdio.h>
int main() {
float x = 4.0;
printf("%f\n",x);
printf("%d\n",x);
int y=x;
printf("%d\n",y);
return 0;
}
gcc 编译器上的输出
4.000000
0
4
据我所读,当我们将 float 分配给 int 变量时,变量的小数部分将终止,然后分配给 int。
为什么在这种情况下没有发生?
Please help me in understanding the following C Output:
#include<stdio.h>
int main() {
float x = 4.0;
printf("%f\n",x);
printf("%d\n",x);
int y=x;
printf("%d\n",y);
return 0;
}
Ouput on gcc compiler
4.000000
0
4
As far as i have read when we assign float to an int variable the decimal part of the variable is terminated and then assigned to the int.
Why it is not happening in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您没有打印
y
,而是再次打印x
。附带说明一下,
printf
无法进行转换。因此,当需要%d
时传递浮点数是未定义的行为。You're not printing
y
, you're printingx
again.As a side note,
printf
can't do conversions. So passing a float when a%d
is expected is undefined behavior.语句是正确的,但要进行此转换,您必须
cast
想要转换的变量,而不仅仅是将其分配给另一个 int 变量。这也会在
printf
语句中导致问题,因为您使用 int 说明符%d
打印一个 float 变量,而没有进行强制转换。修复您的代码如下:
它将正确打印变量
x
的整数值4
。Statement is correct, but to make this conversion you must
cast
variable you wish to convert, not just assign it to another int variable.This causes problems also in the
printf
statement, since you are printing a float variable with int specifier%d
without a cast.Fix you're code like this:
and it will correctly print integer value
4
for variablex
.当作为
...
参数传递时,floats
会自动提升为doubles
(类似于chars
和短整型
提升为整型
)。当printf()
查看格式说明符(%d
或%f
或其他)时,它会抓取并解释收到的原始数据根据格式说明符(如int
或double
或其他),然后打印它。printf("%d\n",x)
是错误的,因为您将double
传递给printf()
但谎称它是将是一个int
。printf()
让你为谎言付出代价。它从其参数中获取 4 个字节(最有可能,但不一定是 4 个字节),这是 int 的大小,它从您之前放置 8 个字节的位置获取这些字节(同样,最有可能的是,但不一定是 8 个字节)的double
4.0,其中这 4 个字节恰好全为零,然后它将它们解释为整数并打印它。事实上,IEEE-754 双精度格式中的 2 的幂通常有 52 个零位,或者超过 6 个 8 位字节为零。这些“最有可能,但不一定”的词意味着 C 标准不强制类型的固定大小和范围,并且它们可能因编译器和操作系统的不同而有所不同。如今,4 字节整数和 8 字节双精度数是最常见的(如果我们考虑 x86 平台)。
floats
are automatically promoted todoubles
when passed as...
parameters (similarly to howchars
andshort ints
are promoted toints
). Whenprintf()
looks at a format specifier (%d
or%f
or whatever), it grabs and interprets the raw data it has received according to the format specifier (asint
ordouble
or whatever) and then prints it.printf("%d\n",x)
is wrong because you are passing adouble
toprintf()
but lie to it that it's going to be anint
.printf()
makes you pay for the lie. It takes 4 bytes (most likely, but not necessarily 4 bytes) from its parameters, which is the size of anint
, it grabs those bytes from where you have previously put 8 bytes (again, most likely, but not necessarily 8 bytes) of thedouble
4.0, of which those 4 bytes happen to be all zeroes and then it interprets them as an integer and prints it. Indeed, powers of 2 in the IEEE-754 double precision format normally have 52 zero bits, or more than 6 8-bit bytes that are zero.Those "most likely, but not necessarily" words mean that the C standard does not mandate a fixed size and range for types and they may vary from compiler to compiler, from OS to OS. These days 4-byte
ints
and 8-bytedoubles
are the most common (if we consider, e.g. the x86 platform).