How to avoid pressing Enter with getchar() for reading a single character only?

后端 未结 12 1697
长发绾君心
长发绾君心 2020-11-22 00:33

In the next code:

#include 

int main(void) {   
  int c;   
  while ((c=getchar())!= EOF)      
    putchar(c); 
  return 0;
}
12条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 01:02

    This code worked for me. Attention : this is not part of the standard library, even if most compilers (I use GCC) supports it.

    #include 
    #include 
    int main(int argc, char const *argv[]) {
        char a = getch();
        printf("You typed a char with an ASCII value of %d, printable as '%c'\n", a, a);
        return 0;
    }
    

    This code detects the first key press.

提交回复
热议问题