getchar and putchar

后端 未结 6 1989
耶瑟儿~
耶瑟儿~ 2021-02-06 14:40

My C code:

int c;
c = getchar();

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

Why does this program react like this on inputting

6条回答
  •  无人共我
    2021-02-06 15:08

    Because the default for stdin when it refers to the keyboard is to be line buffered.
    That means you only get to see full lines, and not single characters.

    Imagine you ask a friend of yours what his phone number is ... but he must write it down on a piece of paper. You don't get the number digit-by-digit as he writes them: you get all of the number when he gives you the piece of paper :)

提交回复
热议问题