What is the standard input buffer?

后端 未结 1 1028
小鲜肉
小鲜肉 2020-12-19 17:53
#include 

int main(void)
{
    int c;
    c = getchar();
    putchar(c);
    c = getchar();
    putchar(c);
    c = getchar();
    putchar(c);
    re         


        
相关标签:
1条回答
  • 2020-12-19 18:22

    This is a feature of your terminal (the command line window).

    By default, the terminal will collect input from the user until he presses Enter/Return. Then the whole line is pushed to the input filestream of your program (stdin, that is; since you use <stdio.h> rather than <iostream>, there's no cin involved).

    This is useful because your program does not have to deal with interpreting all keyboard events (e.g. remove letters when Backspace is pressed). Programs which want to handle the keyboard themselves can disable this default input mode. I think the relevant Google keywords for that are terminfo or termcap.

    Specifically concerning your question, one line of input is good for three getchar() calls if it contains three characters. If you entered only one character, your program should wait on the subsequent getchar() calls for more input.

    0 讨论(0)
提交回复
热议问题