C语言 如何在C中暂停?

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

How to pause in C?

c

提问by CathyLu

I am a beginner of C. I run the C program, but the window closes too fast before I can see anything. How can I pause the window?

我是 C 的初学者。我运行 C 程序,但是在我看不到任何东西之前窗口关闭得太快了。如何暂停窗口?

回答by Gavin H

you can put

你可以放

getchar();

before the return from the main function. That will wait for a character input before exiting the program.

在从主函数返回之前。这将在退出程序之前等待字符输入。

Alternatively you could run your program from a command line and the output would be visible.

或者,您可以从命令行运行程序,输出将可见。

回答by George

If you want to just delaythe closing of the window without having to actually press a button (getchar()method), you can simply use the sleep()method; it takes the amount of secondsyou want to sleep as an argument.

如果只想延迟窗口关闭而不必实际按下按钮(getchar()方法),则可以简单地使用该sleep()方法;它需要您想要睡眠的秒数作为参数。

#include <unistd.h>
// your code here
sleep(3); // sleep for 3 seconds

References: sleep() manual

参考资料: sleep() 手册

回答by Dave

Is it a console program, running in Windows? If so, run it from a console you've already opened. i.e. run "cmd", then change to your directory that has the .exe in it (using the cd command), then type in the exe name. Your console window will stay open.

它是在 Windows 中运行的控制台程序吗?如果是这样,请从您已经打开的控制台运行它。即运行“cmd”,然后切换到包含 .exe 的目录(使用 cd 命令),然后输入 exe 名称。您的控制台窗口将保持打开状态。

回答by akashbhatia

getch()can also be used which is defined in conio.h.

getch()也可以使用在 conio.h 中定义的。

The sample program would look like this :

示例程序如下所示:

#include <stdio.h>
#include <conio.h>

int main()
{
    //your code 
    getch();
    return 0; 
} 

getch()waits for any character input from the keyboard (not necessarily enter key).

getch()等待来自键盘的任何字符输入(不一定是输入键)。

回答by vinc17

Under POSIX systems, the best solution seems to use:

在 POSIX 系统下,最好的解决方案似乎是使用:

#include <unistd.h>

pause ();

If the process receives a signal whose effect is to terminate it (typically by typing Ctrl+Cin the terminal), then pausewill not return and the process will effectively be terminated by this signal. A more advanced usage is to use a signal-catching function, called when the corresponding signal is received, after which pausereturns, resuming the process.

如果进程收到一个信号,其效果是终止它(通常通过在终端中键入Ctrl+ C),则pause不会返回并且进程将被该信号有效地终止。更高级的用法是使用信号捕捉函数,在接收到相应信号时调用,之后pause返回,恢复进程。

Note: using getchar()will not work is the standard input is redirected; hence this more general solution.

注意:使用getchar()将不起作用是标准输入被重定向;因此这个更通用的解决方案。

回答by r1cebank

If you are making a console window program, you can use system("pause");

如果你正在制作一个控制台窗口程序,你可以使用 system("pause");

回答by mfonda

I assume you are on Windows. Instead of trying to run your program by double clicking on it's icon or clicking a button in your IDE, open up a command prompt, cd to the directory your program is in, and run it by typing its name on the command line.

我假设您使用的是 Windows。不要尝试通过双击它的图标或单击 IDE 中的按钮来运行您的程序,而是打开命令提示符,cd 到您的程序所在的目录,然后通过在命令行上键入其名称来运行它。

回答by Daniel Gelling

You could also just use system("pause");

你也可以使用 system("pause");

回答by Tony Hudson

Good job I remembered about DOS Batch files. Don't need Getchar() at all. Just write the batch file to change directory (cd) to the folder where the program resides. type the name of the exe program and on the next line type pause. example:

干得好,我记得 DOS 批处理文件。根本不需要 Getchar() 。只需写入批处理文件即可将目录 (cd) 更改为程序所在的文件夹。键入 exe 程序的名称,然后在下一行键入 pause。例子:

cd\

wallpaper_calculator.exe pause

wall_calculator.exe 暂停

回答by The Link

For Linux; getchar() is all you need.

对于 Linux;getchar() 就是你所需要的。

If you are on Windows, check out the following, it is exactly what you need!

如果您使用的是 Windows,请查看以下内容,这正是您所需要的!

kbit() function

kbit() 函数

  • kbhit() function is used to determine if a key has been pressed or not.
  • To use kbhit() in C or C++ prorams you have to include the header file "conio.h".
  • kbhit() 函数用于确定某个键是否被按下。
  • 要在 C 或 C++ 程序中使用 kbhit(),您必须包含头文件“conio.h”。

For example, see how it works in the following program;

例如,看看它在下面的程序中是如何工作的;

//any_key.c

#include <stdio.h> 
#include <conio.h>

int main(){

    //code here
    printf ("Press any key to continue . . .\n");
    while (1) if (kbhit()) break; 
    //code here 
    return 0;

}

When I compile and run the program, thisis what I see.

当我编译并运行程序时,就是我所看到的。

Only when user presses just one key from the keyboard, kbhit()returns 1.

只有当用户从键盘上只按下一个键时,kbhit() 才返回 1。