我的C代码在scanf语句后没有响应

时间:2014-02-19 04:04:01

标签: c scanf

我编写了一组代码,用于扫描值并使用它们来测试中心极限定理。但是当我在使用scanf输入所有值后运行程序时,我的程序没有进入下一行代码:问题如下:

printf("*** DEMONSTRATION OF CENTRAL LIMIT THEOREM ***");
printf("Enter parameters for the distribution [a b]    ==> ");
scanf("%f %f",&a,&b);
printf("Enter distribution to display [1=data, 2=mean] ==> ");
scanf("%d",&option);
printf("Enter number in each group                     ==> ");
scanf("%d",&group);
printf("Enter number of samples of groups              ==> ");
scanf("%f",&times);
printf("are we here yet");

在这些printf和scanf之后,程序开始进行计算。但是当我编译(成功)后运行程序时。似乎我的代码在scanf(“%f”,& times);

之后被卡住了

“我们在这里”这一行“永远不会被打印”,这意味着程序没有超过scanf。我没有做过很多C编程,这对我来说似乎很奇怪,有人可以弄清楚为什么程序没有超过行scanf(“%f”,& times);我真的很感兴趣

1 个答案:

答案 0 :(得分:2)

终端的输入/输出在C中进行行缓冲,输出不会显示,直到输出换行符,或者调用fflush(stdout),或者程序正常终止并且所有缓冲区都被刷新无论如何。变化:

printf("are we here yet");

为:

printf("are we here yet\n");

或:

printf("are we here yet");
fflush(stdout);

你应该看到你的输出。