C语言 C中的“按任意键继续”功能

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

"Press Any Key to Continue" function in C

c

提问by Sung Min Kim

How do I create a void function that will work as "Press Any Key to Continue" in C?

如何在 C 中创建一个可用作“按任意键继续”的 void 函数?

What I want to do is:

我想做的是:

printf("Let the Battle Begin!\n");
printf("Press Any Key to Continue\n");
//The Void Function Here
//Then I will call the function that will start the game

I'm compiling with Visual Studio 2012.

我正在使用 Visual Studio 2012 进行编译。

回答by Gangadhar

Use C standard library function getchar(),getch() is boreland function not standard one.

使用C标准库函数getchar(),getch()是borland函数,不是标准函数。

only used in windows TURBO C.

仅用于 Windows TURBO C。

printf("Let the Battle Begin!\n");
printf("Press Any Key to Continue\n");  
getchar();    

you should press return key Here. for this the printf statement should be press ENTER to continue.

你应该在这里按回车键。为此,应按 ENTER 继续 printf 语句。

if you press a , then again you need to press ENTER.
if you press ENTER it will continue normally.

如果按 a ,则需要再次按 ENTER。
如果您按 ENTER 它将继续正常。

for this reason, it should be

为此,它应该是

printf("Let the Battle Begin!\n");
printf("Press ENTER key to Continue\n");  
getchar();    


If you are using windows then you can use getch()

如果您使用的是 Windows,那么您可以使用 getch()

printf("Let the Battle Begin!\n");
printf("Press Any Key to Continue\n");
getch();   
//if you press any character it will continue ,  
//but this is not a standard c function.


char ch;
printf("Let the Battle Begin!\n");
printf("Press ENTER key to Continue\n");    
//here also if you press any other key will wait till pressing ENTER
scanf("%c",&ch); //works as getchar() but here extra variable is required.      

回答by Chris Dodd

You don't say what system you're using, but as you already have some answers that may or may not work for Windows, I'll answer for POSIX systems.

您没有说您使用的是什么系统,但由于您已经有了一些可能适用于也可能不适用于 Windows 的答案,我将回答 POSIX 系统。

In POSIX, keyboard input comes through something called a terminal interface, which by default buffers lines of input until Return/Enter is hit, so as to deal properly with backspace. You can change that with the tcsetattr call:

在 POSIX 中,键盘输入通过一种叫做终端接口的东西来,默认情况下,它会缓冲输入行,直到按下 Return/Enter,以便正确处理退格。您可以使用 tcsetattr 调用更改它:

#include <termios.h>

struct termios info;
tcgetattr(0, &info);          /* get current terminal attirbutes; 0 is the file descriptor for stdin */
info.c_lflag &= ~ICANON;      /* disable canonical mode */
info.c_cc[VMIN] = 1;          /* wait until at least one keystroke available */
info.c_cc[VTIME] = 0;         /* no timeout */
tcsetattr(0, TCSANOW, &info); /* set immediately */

Now when you read from stdin (with getchar(), or any other way), it will return characters immediately, without waiting for a Return/Enter. In addition, backspace will no longer 'work' -- instead of erasing the last character, you'll read an actual backspace character in the input.

现在,当您从 stdin(使用getchar()或任何其他方式)读取时,它将立即返回字符,而无需等待 Return/Enter。此外,退格将不再“工作”——您将在输入中读取实际的退格字符,而不是擦除最后一个字符。

Also, you'll want to make sure to restore canonical mode before your program exits, or the non-canonical handling may cause odd effects with your shell or whoever invoked your program.

此外,您需要确保在程序退出之前恢复规范模式,否则非规范处理可能会对您的 shell 或调用您程序的任何人造成奇怪的影响。

回答by Nemanja Boric

Use getch():

使用getch()

printf("Let the Battle Begin!\n");
printf("Press Any Key to Continue\n");
getch();

Windows alternative should be _getch().

Windows 替代方案应该是_getch()

If you're using Windows, this should be the full example:

如果您使用的是 Windows,这应该是完整示例:

#include <conio.h>
#include <ctype.h>

int main( void )
{
    printf("Let the Battle Begin!\n");
    printf("Press Any Key to Continue\n");
    _getch();
}

P.S. as @R?rdnoted, if you're on POSIX system, you need to make sure that curses library is setup right.

PS,@R ?rd指出,如果您使用的是 POSIX 系统,则需要确保 Curses 库设置正确。

回答by Rahul Tripathi

Try this:-

尝试这个:-

printf("Let the Battle Begin!\n");
printf("Press Any Key to Continue\n");
getch();

getch()is used to get a character from console but does not echo to the screen.

getch()用于从控制台获取字符但不回显到屏幕。

回答by Nikolai Egipko

You can try more system indeppended method: system("pause");

您可以尝试更多系统独立的方法: system("pause");