1. After the input function is called, it first checks whether the number of characters in the input buffer has met the input requirements. If yes, it will be read directly from the buffer; if the number of characters in the buffer does not meet the input requirements, wait for the user to enter. The input is usually completed by the user entering the carriage return \ n, then the input function will repeat the previous step-check whether the number of characters in the input buffer meets the input requirements, if yes... if not...
Example:
#include<stdio.h>void main(void){int v1,v2;scanf("%d%d",&v1,&v2); printf("scanf end\n"); return;}
Gcc-o t test. c
./T
1 // The program enters the waiting for input status. We enter 1 and press Enter.
// The program is still waiting for input, because only one "1" in the buffer is detected, and scanf requires two integers. Enter here
1 // The program is still waiting for input. We enter an integer 1 and press Enter.
Scanf end // the scanf function completes reading and returns
Shell returned 2
However, the getc () function does not use \ n as the input completion mark. As long as the user inputs, it starts processing immediately and returns the result.
2. All Input Functions share an input buffer.
Example:
#include<stdio.h>void main(void){int v1,v2;scanf("%d%d",&v1,&v2);printf("%d",getchar());return;}
Gcc-o t test. c
./T
1 2
10 // output the Enter key ascii code, 10
./T
1 2 3
32 // The "3" ascii code is not output here, because "2" is followed by spaces, while getchar () does not skip spaces.
Shell returned 2
It is also seen that the carriage return is a special input, which sends a "user input completed" notification to the input function, and also leaves a "\ n" character in the input buffer.