我已经编译了一个C程序,并准备好了一个a.exe。
现在每当我点击a.exe时,
命令提示符窗口就会打开,
a.exe运行,
然后自动关闭该窗口。
我应该在程序或其他地方做什么,这样在运行a.exe后,命令提示符窗口不会自动关闭。
a.exe
pause
接着开始批处理文件
作为一种替代方案,您可以在C程序中添加一行代码,在退出之前提示用户输入。
在 main()
函数的 return
语句之前使用 system("PAUSE");
。
getchar()
函数来读取stdin
中的单个字符:#include <stdio.h>
int main(int argc,char** argv) {
// getchar() waits until its able to read a character from stdin
getchar();
return 0;
}
你可以从命令提示符中运行程序:
按下win键+R打开“运行”提示,然后输入cmd
。然后按回车键。现在使用cd
命令导航到程序所在的目录,并从那里运行它。
或者,您可以将此添加到程序的末尾:
#include <stdio.h>
int main() {
// ...
getchar();
return 0;
}
cin.get()
或等效方法、system("PAUSE");
、从命令行运行或批处理文件。 - Seth Carnegie