[英]When i try to give 3 inputs to my program , it needs 4 to run properly. Why is that?
#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上,因为我在你的程序中看到了system("pause")
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.