windows “不要让 cmd 窗口自动关闭” - 我该怎么做?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8276950/
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-15 18:32:01  来源:igfitidea点击:

"Dont let cmd window to close automatically" -how can i do this?

cwindowscmd

提问by Jeegar Patel

see i have compile one c program and prepare one a.exe

看到我已经编译了一个 c 程序并准备了一个 a.exe

Now when ever i click on a.exe

现在每当我点击 a.exe

cmd windows opens

cmd窗口打开

a.exe run

a.exe 运行

and automatically close that windows.

并自动关闭该窗口。

What should i do in program or anywhere so this way after running that a.exe that cmd windows don't close automatically.

在运行 cmd 窗口不会自动关闭的 a.exe 之后,我应该在程序中或任何地方以这种方式做什么

回答by a_horse_with_no_name

Create a batch file with the following content:

创建一个包含以下内容的批处理文件:

a.exe
pause

Then start the batch file

然后启动批处理文件

As an alternative you could add a line to your C program that prompts the user for input before exiting.

作为替代方案,您可以在 C 程序中添加一行,在退出之前提示用户输入。

回答by mtahmed

Use system("PAUSE");just before the returnin your main().

使用system("PAUSE");之前只是return在你的main()

回答by Cyclonecode

You could use getchar()which will read a single character from stdin:

您可以使用getchar()which 将从中读取单个字符stdin

#include <stdio.h>

int main(int argc,char** argv) {
  // getchar() waits until its able to read a character from stdin
  getchar();
  return 0;
}

回答by David Hu

You can run your program from the command prompt:

您可以从命令提示符运行您的程序:

Press winkey + R to open up the runprompt, and type in cmd. Then press enter. Now navigate to the directory where your program is by using the cdcommand, and run it from there.

按 winkey + R 打开run提示,然后输入cmd. 然后按回车。现在使用cd命令导航到您的程序所在的目录,并从那里运行它。

Alternatively, you can add this to the end of your program:

或者,您可以将其添加到程序的末尾:

#include <stdio.h>

int main() {
     // ...    
     getchar();
     return 0;    
}