当我在 cmd 中运行用于计算数字平方的 C 代码时,它工作正常,但是当我双击它时,它会显示“输入任意数字”,但只要我按下一个键,exe 文件就会关闭。
这是我的代码:
int main( ){
float square ( float ) ;
float a, b ;
printf ( "\nEnter any number " ) ;
scanf ( "%f", &a ) ;
b = square ( a ) ;
printf ( "\nSquare of %f is %f", a, b ) ;
return 0;
}
float square ( float x ){
float y ;
y = x * x ;
return ( y ) ;
}
最佳答案
当您双击该程序时,它将执行,然后关闭,因为它已完成。您可以在主函数末尾添加这些行,以在完成之前等待按键输入:
char ch;
scanf("%c",&ch);
所以它会变成这样:
int main( ){
float square ( float ) ;
float a, b ;
printf ( "\nEnter any number " ) ;
scanf ( "%f", &a ) ;
b = square ( a ) ;
printf ( "\nSquare of %f is %f", a, b ) ;
// Wait for an input to finish the execution
char ch;
scanf("%c",&ch);
return 0;
}
float square ( float x ){
float y ;
y = x * x ;
return ( y ) ;
}
关于c - 为什么我的 exe 文件在 cmd 中可以正常工作,但双击却无法正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56635168/