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

后端 未结 12 1618
长发绾君心
长发绾君心 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:14

    By default, the C library buffers the output until it sees a return. To print out the results immediately, use fflush:

    while((c=getchar())!= EOF)      
    {
        putchar(c);
        fflush(stdout);
    }
    

提交回复
热议问题