为什么我的程序需要4个输入才能正常运行,尽管我提供了3个?
- 内容介绍
- 文章标签
- 相关推荐
本文共计220个文字,预计阅读时间需要1分钟。
当然可以,请您提供需要改写的原文,我会根据您的要求进行修改。

#include <stdio.h>
#include <stdlib.h>
#define f(x) (1 / (x*x+1))
int main(){
double a,b,h,x,y;
printf("Enter a, b, h: ");
scanf(" %lf %lf %lf " , &a, &b, &h);
// I ask for 3 inputs but the programm needs 4 to run...why is that?
x = a;
while(x<b)
{
y = f(x);
printf("%lf %lf \n", x ,y );
x +=h;
}
system("Pause");
return(0);
}
问题出在你的scanf上:
scanf(" %lf %lf %lf " , &a, &b, &h); ^
scanf需要查看下一个非空格来确定这个“0或更多空格”的结束,所以你必须给第四个值(它可以是垃圾 – 只要它不是空格),以便scanf终止输入.

如果您在Windows上,可以在新行上按Ctrl-Z并按Enter键.这将向程序发送EOF,也可以终止输入. (我想你是在Windows上,因为我在你的程序中看到了系统(“暂停”))
本文共计220个文字,预计阅读时间需要1分钟。
当然可以,请您提供需要改写的原文,我会根据您的要求进行修改。

#include <stdio.h>
#include <stdlib.h>
#define f(x) (1 / (x*x+1))
int main(){
double a,b,h,x,y;
printf("Enter a, b, h: ");
scanf(" %lf %lf %lf " , &a, &b, &h);
// I ask for 3 inputs but the programm needs 4 to run...why is that?
x = a;
while(x<b)
{
y = f(x);
printf("%lf %lf \n", x ,y );
x +=h;
}
system("Pause");
return(0);
}
问题出在你的scanf上:
scanf(" %lf %lf %lf " , &a, &b, &h); ^
scanf需要查看下一个非空格来确定这个“0或更多空格”的结束,所以你必须给第四个值(它可以是垃圾 – 只要它不是空格),以便scanf终止输入.

如果您在Windows上,可以在新行上按Ctrl-Z并按Enter键.这将向程序发送EOF,也可以终止输入. (我想你是在Windows上,因为我在你的程序中看到了系统(“暂停”))
