为什么 printf 隐式 float 到 int 转换不起作用?

发布于 2024-12-21 16:05:06 字数 339 浏览 2 评论 0原文

请帮助我理解以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彻夜缠绵 2024-12-28 16:05:06

您没有打印 y,而是再次打印 x

附带说明一下,printf 无法进行转换。因此,当需要 %d 时传递浮点数是未定义的行为。

You're not printing y, you're printing x again.

As a side note, printf can't do conversions. So passing a float when a %d is expected is undefined behavior.

桃气十足 2024-12-28 16:05:06

据我所知,当我们将 float 分配给 int 变量时
变量的小数部分终止,然后分配给
整数。

语句是正确的,但要进行此转换,您必须 cast 想要转换的变量,而不仅仅是将其分配给另一个 int 变量。

这也会在 printf 语句中导致问题,因为您使用 int 说明符 %d 打印一个 float 变量,而没有进行强制转换。

修复您的代码如下:

printf("%d\n", (int) x);
                 ^
                 | this is the cast operation

它将正确打印变量 x 的整数值 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.

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:

printf("%d\n", (int) x);
                 ^
                 | this is the cast operation

and it will correctly print integer value 4 for variable x.

假扮的天使 2024-12-28 16:05:06

当作为 ... 参数传递时,floats 会自动提升为 doubles(类似于 chars短整型 提升为整型)。当 printf() 查看格式说明符(%d%f 或其他)时,它会抓取并解释收到的原始数据根据格式说明符(如 intdouble 或其他),然后打印它。

printf("%d\n",x) 是错误的,因为您将 double 传递给 printf() 但谎称它是将是一个intprintf() 让你为谎言付出代价。它从其参数中获取 4 个字节(最有可能,但不一定是 4 个字节),这是 int 的大小,它从您之前放置 8 个字节的位置获取这些字节(同样,最有可能的是,但不一定是 8 个字节)的 double 4.0,其中这 4 个字节恰好全为零,然后它将它们解释为整数并打印它。事实上,IEEE-754 双精度格式中的 2 的幂通常有 52 个零位,或者超过 6 个 8 位字节为零。

这些“最有可能,但不一定”的词意味着 C 标准不强制类型的固定大小和范围,并且它们可能因编译器和操作系统的不同而有所不同。如今,4 字节整数和 8 字节双精度数是最常见的(如果我们考虑 x86 平台)。

floats are automatically promoted to doubles when passed as ... parameters (similarly to how chars and short ints are promoted to ints). When printf() 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 (as int or double or whatever) and then prints it.

printf("%d\n",x) is wrong because you are passing a double to printf() but lie to it that it's going to be an int. 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 an int, it grabs those bytes from where you have previously put 8 bytes (again, most likely, but not necessarily 8 bytes) of the double 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-byte doubles are the most common (if we consider, e.g. the x86 platform).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文