C 如何实现不用回车,按任意键继续运行程序

学习自:https://docs.pingcode.com/ask/305563.html

我通常接收键盘输入一个字符时使用的是getchar();

但getchar()有个特点就是它可以反复修改且需要回车确认;

但有的场景下我们不需要反复修改和回车确认时,这个函数就不好用了;

getch()是不可以反复修改和回车确认的;

刚好可以在这种场景下替代getchar()

#include "stdio.h"
int main(int argc, char const *argv[])
{
    int a=getch();
    printf("\e[32m[%s %d]: %c\e[0m\n",__FUNCTION__,__LINE__,a);
    return 0;
}

 

posted @ 2025-07-15 08:19  败人两字非傲即惰  阅读(41)  评论(0)    收藏  举报