C - scanf() 没有为输入的双精度值给出正确的值


double low = 0 , high = 0.0,n = 0.0;
----
printf("Enter the integral limits (low high): ");
scanf("%lf %lf", &low, &high);
printf("%lf%lf",low,high);
printf("Enter the number of subinternals (n): ");
scanf("%lf",&n);

当我测试这个程序时(通过

    printf("%lf%lf",low,high);) (6th line)
我得到低和高的值为 0.000000

和 0.000000。我在程序开始时初始化了它们,现在我正在尝试为它们输入值。

我做错了什么?

{附言我在一台Windows 7笔记本电脑上。我已经尝试将"%f"更改为"%lf"并返回"%f")

我该怎么办?(请帮忙!

编辑:我不确定刚刚发生了什么。我错误检查了

        if (2 != scanf("%lf %lf", &low, &high))
               printf("scanf failed!n")

相反,发生的事情是在我的"..(低高):"我输入了 1 和 2,然后程序正在等待更多输入,然后我再次输入 1 和 2,这一次,它给出了正确的原则......不知道发生了什么?

输出屏幕:http://s1288.photobucket.com/user/Kdragonflys/media/ss_zps6b85396e.png.html

你需要%lf for double s with scanf ,如果你有两个double要读取,你还需要其中两个。

所以改变:

scanf("%f", &low, &high);

自:

scanf("%lf %lf", &low, &high);

还更改:

scanf("%f",&n);

自:

scanf("%lf",&n);

注意:错误检查始终是一个好主意 - scanf返回成功处理的值数。因此,您可以检查第一个scanf调用是否像这样工作:

if (scanf("%lf %lf", &low, &high) != 2)
    printf("scanf failed!n");

试试这个,

double low = 0.0 , high = 0.0,n = 0.0;
----
printf("Enter the integral limits (low high): ");
scanf("%lf %lf", &low, &high);
printf("%lf %lf",low,high);
printf("Enter the number of subinternals (n): ");
scanf("%lf",&n);

最新更新