在c语言中,在不干扰程序执行的情况下获取输入

在c语言中,在不干扰程序执行的情况下获取输入,c,io,C,Io,我希望在循环中获取输入而不停止其执行,即 ` #include<stdio.h> #include<stdlib.h> #include<conio.h> int main() { char c; while(1) { if((c=getch())=='y') printf("yes\n") ; printf("no\n") ;

我希望在循环中获取输入而不停止其执行,即

`

   #include<stdio.h>
   #include<stdlib.h>
   #include<conio.h>
   int main()
   {
    char c;
      while(1)
      {

        if((c=getch())=='y')
                printf("yes\n") ;

        printf("no\n") ;
     }
      return 0;
   }
#包括
#包括
#包括
int main()
{
字符c;
而(1)
{
如果((c=getch())=='y')
printf(“是\n”);
printf(“否”);
}
返回0;
}

现在我希望无论输入如何,都能无限地打印“否”,如果我按y键,那么应该打印“是”,然后再从“否”继续。这是可能的,任何想法

由于这似乎是在Windows上,并且您已经在使用旧的conio函数,因此可以使用
\u kbhit()

#包括
#包括
#包括
int main()
{
而(1)
{
如果(_kbhit()&&getch()=='y')
printf(“是\n”);
printf(“否”);
}
返回0;
}

\u kbhit()
“检查控制台是否有最近的击键,”根据。这意味着,如果
\u kbhit()
为真,
getch()
将能够立即获取字符,而不会阻塞。

getch()将在读取时阻塞。要执行所需操作,请尝试在STDIN_FILENO.Threading上执行非阻塞读取-----在一个线程中打印
no
,然后在一个线程中打印
yes
another@BhargavRao:胡说八道。这甚至不是一个合理的建议。您可以将
select()
poll()
getch()
结合使用,以确保它是非阻塞的。@BhargavRao:1)这是C,没有太多线程支持。2) 线程甚至不是解决方案的可行建议,因为它与线程将解决的任何问题都无关。
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

int main()
{
  while(1)
  {
    if(_kbhit() && getch() == 'y')
      printf("yes\n");

    printf("no\n") ;
  }
  return 0;
}