C语言输入输出不一致

Xcode 编译C时 输入输出不一致

问题相关代码,请勿粘贴截图

#include <stdio.h>
//测试输入输出不一致问题
int main(void)
{
float a;
printf("input a num:\n");
scanf("%f",&a);
printf("%f\n",a);
printf("There are %f seconds in %f years old\n",a*3.156e+7,a);
return 0;
}

运行结果及报错内容

input a num:
9.9
9.000000
There are 284040000.000000 seconds in 9.000000 years old
Program ended with exit code: 0
保存最新程序,重新试了几次
input a num:
9.9
0.000000
There are 0.000000 seconds in 0.000000 years old
Program ended with exit code: 0
也有正确的
input a num:
9.9
9.900000
There are 312443987.960815 seconds in 9.900000 years old
Program ended with exit code: 0

我这是用的Mac XCode编译器编辑,是不是有关系?

我的解答思路和尝试过的方法

在输入2.2等其他数据时输出0.000000,why?

我想要达到的结果

这似乎没有道理啊。程序修改后保存了吗?

精度被发生了改变,单精度型在参与运算时或作其它输出时精度被自动升为双精度型,当你还试着用%f作为输出修饰符时就会发生错误!尝试用双精度型试试。

img

img

img


#include <stdio.h>
//测试输入输出不一致问题
int main(void)
{
float a;
printf("input a num:\n");
scanf("%f",&a);
printf("%lf\n",a);
printf("There are %lf seconds in %lf years old\n",a*3.156e+7,a);

return 0;
}