Getchar ()
1. reading a character from the buffer zone is equivalent to clearing the Buffer Zone 2. when reading the input, scanf () will leave a character '\ n' in the buffer (after entering the value of S [I], press the Enter key ), therefore, if you do not add a getchar () here to remove the carriage return, gets () will not wait for the character to be typed on the keyboard, but will directly remove the "useless" carriage return, as a result, the read error occurs. 3. getchar () is inconvenient to read a character (including spaces, carriage return, and Tab) in the input buffer sequence. Solution: (1) Clear the carriage return using the following statement: while (getchar ()! = '\ N'); (2) use getche () or getch () instead of getchar (). The function is to read a character from the keyboard (do not press Enter ), note that the header file <conio. h> 4. [convert] getchar () is stdio. the library function in H reads a character from the stdin stream. That is to say, if stdin has data, it can be directly read without entering it. For the first time getchar (), manual input is required, but if you lose multiple characters, later, when getchar () is executed, it will be directly read from the buffer. Actually: Enter the device-> memory buffer-> program getchar. The key you press is put into the buffer, for the program getchar, have you tried to hold down a lot of keys and wait for a while before Didi will ring, that is, the buffer is full, and the keys you press in the back are not saved in the buffer. All the characters entered by the keyboard are stored in the buffer. Once you press enter, getchar enters the buffer to read the characters. Only the first character is returned at a time as the value of the getchar function, if there are loops or enough getchar statements, all the characters in the buffer will be read in sequence until '\ n' (this' \ n' will also be read ). To understand this, the reason why you input a series of characters is read in sequence is that the function of loop allows the repeated use of getchar to read characters in the buffer, rather than getchar to read multiple characters, in fact, getchar can only read one character at a time. if you want to cancel the effect of '\ n', you can use getchar (); to clear it. Here, getchar (); only gets' \ n' but does not assign it to any character variable, so there is no effect, which is equivalent to clearing this character. note that the echo you see in ssss on the keyboard is exactly the function of getchar. If you use getch, you will not be able to see what you entered.
Getchar () Usage