C语言 如何防止控制台(应用程序文件)在 Windows 中运行后立即关闭?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36188896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:22:51  来源:igfitidea点击:

How do i prevent the console (application file) from closing itself immediately after running in windows?

c

提问by Ozgur

After I compile my program and run the .exe one, it closes after first run. I barely see my output. Do I need codes to prevent that, if so what are these codes?

在我编译我的程序并运行 .exe 之后,它在第一次运行后关闭。我几乎看不到我的输出。我是否需要代码来防止这种情况发生,如果需要,这些代码是什么?

回答by conrad gardner

The following two lines will print the message "Press ENTER key to Continue" and then pause execution waiting for the enter key. I hope this helps.

以下两行将打印消息“Press ENTER key to Continue”,然后暂停执行等待回车键。我希望这有帮助。

printf("Press ENTER key to Continue\n");  
getchar(); 

回答by ΦXoc? ? Пepeúpa ツ

The problem is quite common when starting to learn C/C++..

这个问题在开始学习 C/C++ 时很常见。

the reason is that console applications once finisher return from their main method, the associated console window automatically closes. This behavior has nothing to do with what your app does or not, or if the app is working well or not.

原因是控制台应用程序一旦完成程序从它们的主要方法返回,相关的控制台窗口就会自动关闭。这种行为与您的应用程序做什么或不做什么,或者应用程序是否运行良好无关。

To "correct" this simple add a pause just before the return statement in the main method

要“纠正”这个简单的方法,请在 main 方法中的 return 语句之前添加一个暂停

Example:

例子:

....
   system("pause");  
   return 0;
}

回答by Dendi Suhubdy

Do this

做这个

int main()
{

   // your code

   system("pause"); // this will stop the pause 

   return 0;
}

回答by Mihov

1)Try to run it natively through the cmd.

1)尝试通过cmd本地运行它。

Let's say that your file is in C:\file.exe Open the cmd, type cd C:\ and then type file.exe

假设你的文件在 C:\file.exe 打开 cmd,输入 cd C:\ 然后输入 file.exe

2)Try to add "system("PAUSE");" to your program to keep it opened.

2)尝试添加“系统(“暂停”);” 到您的程序以使其保持打开状态。