【发布时间】:2015-09-04 05:39:12
【问题描述】:
我在 windows 7 上使用 MinGW 来编译 C 文件。
我的问题是 scanf() 从用户输入中读取 doubles 的奇怪行为。
我的代码:
int main() {
double radius = 0;
double pi = 3.14159;
scanf("%lf \n", &radius); // after the input, it continues waiting...
radius = ((radius * radius) * pi);
printf("A=%.4lf\n", radius);
return 0;
}
当我运行这个程序时,需要输入一个值,假设100.64,正常行为是按回车,程序应该继续并显示结果,但程序一直等待更多输入。如果我输入 0 并再次按回车,程序将继续正常运行。
>area.exe
100.64 <-- doesn't proceed after press enter
0 <-- needs input another value, then press enter
A=31819.3103 <-- the result
为什么 scanf 不继续第一个输入?为什么它需要更多?
Obs:在我的 Linux 中不会发生这种情况。
gcc --version
gcc (tdm64-1) 4.9.2
【问题讨论】:
-
我可以建议
int main()-->int main(void)吗? -
@SouravGhosh 为什么?这种情况有区别吗?
-
其实后者是
C标准推荐的。
标签: c gcc double user-input scanf