C语言 C 在按键时退出无限循环

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

C exit from infinite loop on keypress

cinfinite-loopgetch

提问by Ragnis

How can I exit from an infinite loop, when a key is pressed? Currently I'm using getch, but it will start blocking my loop as soon, as there is no more input to read.

按下某个键时,如何退出无限循环?目前我正在使用 getch,但它会尽快开始阻塞我的循环,因为没有更多的输入可供读取。

采纳答案by Talha Ahmed Khan

I would suggest that you go throgh this article.

我建议你去看这篇文章。

Non-blocking user input in loop without ncurses.

无 ncurses 循环中的非阻塞用户输入。

回答by Rudy Velthuis

If you are using getch()from conio.hanyway, try to use kbhit()instead. Note that both getch()and kbhit()- conio.h, in fact - are not standard C.

如果您正在使用getch()from conio.h,请尝试使用kbhit()。请注意,getch()kbhit()-conio.h事实上 - 都不是标准的 C。

回答by taskinoor

The function kbhit()from conio.hreturns non-zero value if any key is pressed but it does not block like getch(). Now, this is obviously not standard. But as you are already using getch()from conio.h, I think your compiler has this.

如果按下任何键,则函数kbhit()fromconio.h返回非零值,但它不会像getch(). 现在,这显然不是标准的。但是由于您已经在使用getch()from conio.h,我认为您的编译器具有此功能。

if (kbhit()) {
    // keyboard pressed
}

From Wikipedia,

来自维基百科

conio.h is a C header file used in old MS-DOS compilers to create text user interfaces. It is not described in The C Programming Language book, and it is not part of the C standard library, ISO C nor is it required by POSIX.

Most C compilers that target DOS, Windows 3.x, Phar Lap, DOSX, OS/2, or Win321have this header and supply the associated library functions in the default C library. Most C compilers that target UNIX and Linux do not have this header and do not supply the library functions.

conio.h 是旧 MS-DOS 编译器中用于创建文本用户界面的 C 头文件。The C Programming Language 一书中没有描述它,它不是 C 标准库、ISO C 的一部分,也不是 POSIX 所要求的。

大多数面向 DOS、Windows 3.x、Phar Lap、DOSX、OS/2 或 Win32 1 的C 编译器都有这个头文件并在默认 C 库中提供相关的库函数。大多数面向 UNIX 和 Linux 的 C 编译器没有这个头文件,也不提供库函数。

回答by Milind Deore

If you do not want to use non-standard, non-blocking way and yet graceful exit. Use signals and Ctrl+Cwith user provided signal handler to clean up. Something like this:

如果你不想使用非标准的、非阻塞的方式而优雅地退出。使用信号和Ctrl+C以及用户提供的信号处理程序进行清理。像这样的东西:

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>

/* Signal Handler for SIGINT */
void sigint_handler(int sig_num)
{
    /* Reset handler to catch SIGINT next time.
       Refer http://en.cppreference.com/w/c/program/signal */
    printf("\n User provided signal handler for Ctrl+C \n");

    /* Do a graceful cleanup of the program like: free memory/resources/etc and exit */
    exit(0);
}

int main ()
{
    signal(SIGINT, sigint_handler);

    /* Infinite loop */
    while(1)
    {
        printf("Inside program logic loop\n");
    }
    return 0;
}

回答by user6060781

// Include stdlib.h to execute exit function
int char ch;
int i;

clrscr();
void main(){

printf("Print 1 to 5 again and again");
while(1){
for(i=1;i<=5;i++)

     printf("\n%d",i);

    ch=getch();
    if(ch=='Q')// Q for Quit
     exit(0);

    }//while loop ends here

    getch();
    }