-
Most people only look at the getchar name and think that its return value is char type. But getchar is indeed not a char type, but an int type, and its prototype is as follows:
int getchar(void);getchar has an int return value. When the program calls getchar, the program waits for the user to press the key. The characters entered by the user are stored in the keyboard buffer until the user presses Enter (the carriage return characters are also placed in the buffer). When the user types Enter, getchar starts to read one character from the stdio stream at a time. The return value of the getchar function is the ASCII code of the first character entered by the user. If an error returns -1, and the characters entered by the user are echoed to the screen. If the user enters more than one character before pressing Enter, other characters will be retained in the keyboard cache area and wait for subsequent getchar calls to read. That is to say, subsequent getchar calls will not wait for the user to press the key, but will directly read the characters in the buffer until the characters in the buffer are read as soon as they are read. getchar() reads a character in the input buffer (including spaces, carriage return and Tab). Getchar() is inconvenient to use. Solution: (1) Use the following statement to clear the carriage return: while(getchar()!='\n'); (2) Use getche() or getch() instead of getchar(), which is used to read a character from the keyboard (no need to press Enter), and be careful to include the header file <>
-
getchar() is a library function in it. Its function is to read a character from the stdin stream. That is to say, if stdin has data, you can read it directly without inputting it. The first time you getchar() does require manual input, but if you input multiple characters, the future getchar() will be directly read from the buffer when it is executed again. In fact, it is input device->memory buffer->program getchar The key you press is put into the buffer, and then the program getchar Have you tried holding down many keys and waiting for a while and then making a sound? The buffer is full, and the key you pressed later is not stored in the buffer. The characters entered by the keyboard are stored in the buffer. Once you type Enter, getchar enters the buffer to read characters, and only returns the first character at a time as the value of the getchar function. If there is a loop or enough getchar statement, all characters in the buffer will be read out in sequence until '\n'. To understand this, the reason why the series of characters you enter are read out in sequence is because the loop makes it repeatedly use getchar to read characters in the buffer instead of getchar To read multiple characters, in fact, getchar can only read one character at a time. If you need to cancel the effect of '\n', you can use getchar(); to clear it. Here getchar(); it just gets '\n' but is not assigned to any character variables, so there will be no impact, which is equivalent to clearing this character. It should also be noted that the echo you see in sss on the keyboard is exactly the effect of getchar. If you use getchar, you won't see what you entered.
-
Example: #include <>
main()
{
char ch1[10],ch2[10],c1,c2;
scanf("%s",ch1);
c1=getchar();
gets(ch2);
c2=getchar();
}
Type asdfg to enter, asdfg enter, then ch1="asdfg\0", c1='\n', ch2="asdfg\0", c2 needs to be entered.