新人求问,为什么两个停止语句才能停止我用C-FREE 5.0 编写#includestdio.hint main(void){float radius
新人求问,为什么两个停止语句才能停止
我用C-FREE 5.0 编写
#include<stdio.h>
int main(void)
{
float radius=0.0f;
float diameter=0.0f;
float circumference=0.0f;
float area=0.0f;
float pi=3.14159265f;
printf("Input the diameter of the table:");
scanf("%f",&diameter);
radius=diameter/2.0f;
circumference=2.0f*pi*radius;
area=pi*radius*radius;
printf("\nThe circumference is %.2f",circumference);
printf("\nThe area is %.2f\n",area);
getchar();
getchar();
}
以上代码是输入圆形半径计算周长和面积的,后面有两个getchar()语句。
在C-free中直接调试,程序计算完毕后就自动关闭了,加入一个getchar()语句,依然自动关闭,
要两个才能停留在结果的画面,请问这是为什么?
我编译为exe,直接在windows上运行也是一样,两个getchar()语句的exe才不会自动关闭,一个就会自动关闭
[解决办法]
可以换用 system("pause");
[解决办法]
fflush(stdin);//清空缓冲区
getchar();
因为缓冲区里面有遗留字符 给getchar吃了
上例有回车 \n等字符
