c - 无限循环直到按下键

标签 c loops

#include <stdio.h>
void main()
{
    int a=1;
    char c;
    x:for(a=1;a!=0;a++)
    {
        printf("Hello\n");
        c=getch();
        if(c=='n')
            exit(0);
        else
            goto x;
    }
}

//请仅使用主要运算符协助我完成此程序

最佳答案

这有点不同,向您展示一个简单的解决方案。但是,如果不允许您使用 kbhit,您就会被卡住。

#include <stdio.h>
#include <conio.h>              // include the library header

int main(void)                  // correct signature for main
{
    int c = 0;                  // note getch() returns `int` type
    while(c != 'n')             // until correct key is pressed
    {
        do {                    // forever
            printf("Hello\n");
        } while(!kbhit());      // until a key press detected
        c = getch();            // fetch that key press
    }
    return 0;
}

请记住,它只测试小写的 n

关于c - 无限循环直到按下键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40639720/

相关文章:

使用 C 与 mbed 通信

c - 使用动态数组按字典顺序对输入进行排序

java - 真假游戏在第一轮后不起作用

c - 为什么我不能在 while 中使用整个 char[]?

c++ - 结构错误表达式必须有 bool 类型

c - 内存对齐和填充 - 32 位和 64 位之间的差异

c - 如何正确链接 KBuild Makefile 以在内核模块中构建子文件夹

python - 在循环中将计数器增加为字典值

javascript - 在数组对象中查找字符串值的最简洁方法?

C循环双链表: rev traverse gives different list-pointer address for same node