为什么第一和第三的printf工作如此不同? [英] Why do the first and the third printf work so differently?

查看:109
本文介绍了为什么第一和第三的printf工作如此不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

printf("line 5: %f\n",98);  //output is 0.000000
printf("line 6: %f\n",98.98); //output is 98.980000  
printf("line 5: %f\n",98);//though same as first printf statement but output is 98.979980

虽然第一个和最后的printf语句完全一样,但它们的输出不同。为什么呢?

Though first and last printf statements are exactly same, but their output differs. Why?

怎么一回事,因为一个int被传递时,它期待浮动给printf,这就是为什么它的工作怪异。但我的观点是,为什么在过去的打印说明书,而不是打印一些垃圾值或0,这是第二次使用printf语句的价值,那是什么被打印出来。

beacuse an int is being passed to printf when it is expecting float, thats why it worked weird. But my point is why in the last print statment, instead of printing some garbage value or 0, it is using the value of 2nd printf statement and thats what gets printed.

推荐答案

正如其他人已经说过,路过一个 INT 的printf 时,它的期待双击导致不确定的行为,而且什么都可能发生。你可能会感兴趣的原因的为什么的程序打印 98.979980 第三行,而不是一些随机数。

As others have already said, passing an int to printf when it's expecting a double causes undefined behaviour, and anything could happen. You might be interested in the reason why the program prints 98.979980 on the third line and not some random number.

参数传递给的printf 在堆栈中。当2号线通行证 98.98 的printf 它压入堆栈,用的最低显著部分数​​第一。

Arguments are passed to printf on the stack. When line 2 passes 98.98 to printf it is pushed on the stack, with the least significant part of the number first.

然后的printf 回报,并在第三行再次调用,现在用 98 压入堆栈。在您的架构 INT 类型似乎是32位;在的一半大小双类型,这仅覆盖的下半部分98.98 这是在栈早。 98.98的上半部分仍然是在堆栈中。

Then printf returns, and on the third line it is called again, now with 98 pushed on the stack. On your architecture the int type seems to be 32 bits; half the size of the double type, so this overwrites only the lower half of 98.98 that was on the stack earlier. The upper half of 98.98 is still on the stack.

现在到第三调用printf 从堆栈中读取双击。什么它读取最显著的一半来自于 98.98 这是在栈上早了,不太显著一半来自二进制重新presentation 98 ;这就是为什么结果如此接近 98.98 。自98年就是这样的少数最显著位为0,并设置 98.98 大多零的最低显著一半给你一个较小的数字。

Now the third call to printf reads a double from the stack. The most significant half of what it reads comes from the 98.98 that was on the stack earlier, and the less significant half comes from the binary representation of 98; this is why the result is so close to 98.98. Since 98 is such a small number its most significant bits will be 0, and setting the least significant half of 98.98 to mostly zeros gives you a smaller number.

如果3号线采用了一些,有更多的位为1时,你会得到的结果比详细98.98 。例如,-1的二进制重新presentation拥有其所有位为1时,你会得到:

If line 3 used a number that has more bits set to 1 you would get a result that is more than 98.98. For example, the binary representation of -1 has all its bits set to 1, and you get:

printf("line 2: %f\n", 98.98); # 98.98
printf("line 3: %f\n", -1);    # 98.980042

如果编译器使用64位整数,或者通过双击 s的最显著的部分第一,或使用一个寄存器而不是堆栈来传递参数,你会得到非常不同的结果。

If the compiler used 64 bit ints, or passed doubles with the most significant part first, or used a register instead of the stack to pass parameters, you would get very different results.

这篇关于为什么第一和第三的printf工作如此不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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