#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int x, *ptr_x;
float f , *ptr_f;
ptr_f = &f;
ptr_x = &x;
*ptr_x = 5;
*ptr_f = 1.5; //printf("%d %f\n", f,x);
printf ("\n\nxd = %d \t xf = %f \n ff = %f \t fd = %d", x,x,f,f);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
ff =%f的输出不是预期的.
xd = 5 xf = 0.000000
ff = 0.000000 fd = 1073217536
此代码的要点是显示如果使用%d打印浮动值并且打印int值为%f将会发生什么.
为什么即使我使用%f,浮点值也不能正确打印?
可能重复:
使用%f打印整数变量
#include<stdio.h>
int main()
{
printf("%f",9/5);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:0.000000
任何人都可以解释上述程序的输出吗?
不应该是程序的输出是1.000000?
May be this is a simple question but I am not sure about how float variables are stored in memory and why it is behaving in this way, can someone please explain about the following behavior.
#include<stdio.h>
int main ()
{
int a = 9/5;
printf("%f\n", a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
0.000000
Run Code Online (Sandbox Code Playgroud)
I have looked at some information on how float variables are stored in memory, it has stuff about mantissa, exponent and sign. But I am not getting how …
我在处理整数和浮点数时看到打印功能异常。
float y = 9/5;
printf("%f", y);
printf("%f", 9/5);
Run Code Online (Sandbox Code Playgroud)
第一个打印语句输出1.000000,可以接受,而其他输出0.000000。为什么两种情况下的输出都不同?