【发布时间】:2011-12-14 21:04:12
【问题描述】:
#include "stdafx.h"
#include "stdio.h"
#include "math.h"
int main()
{
float c;
printf("Type c");
scanf("%f", &c);
printf("C is c: %f ",c);
while(getchar()!='\n');
getchar();
return 0;
}
非常简单的代码,我刚从 C 开始。 大多数数字都可以,但是例如如果我输入 47.2 那么它会打印出我 47.200001 和 88.4321 输出 88.432098 但其他数字如 5.4 工作正常 5.400000 我正在使用 Microsoft Visual Studio 10。
另外一件事如果上面的代码没有放 float c 我放了 double 我不能 printf 我应该放什么来代替 %f? 但是我的第一个问题更让我担心,也许这与编译器有关也许我正在将c代码编译为c++,我不知道。
【问题讨论】:
-
@MichaelHaidl:根据标准,
%f和%lf在传递给printf()时具有相同的行为,它们只是scanf()不同。