首页 > 解决方案 > 为什么 printf 将整数打印为双精度数?

问题描述

printf("%f", 1.0); //prints 1.0

printf("%f", 1);  // prints 0.0

转换是如何发生的?

标签: cprintf

解决方案


根据以下@Eric Postpischil's评论不同。

The first double argument (or float argument, which will be promoted to double if used with the ... part of a function) is put in %xmm0. The first “normal integer class” type of argument would go into %rdi. For printf, though, that pointer to the format string is the first argument of that class, so it does into %rdi. That means the first int argument passed goes into the second register for that class, which is %rsi. So, with printf("%f", 1);, printf looks for the floating-point value in %xmm0, but the caller puts an int 1 in %rsi


推荐阅读