In the C language, if the character type variable (that is, CHAR type), it is easy to cause the process to cause the program due to the spam of spam. Look below, simple code: #include "stdio.h"
Main () {Char A, B; Printf ("INPUT A:"); Scanf ("% C", & A); / * or a = getchar (); * / printf ("a =% C / N", a); Printf ("INPUT B:"); Scanf ("% C", & b); / * or b = getchar (); * / printf ("b =% C / N", b);} The code is really simple, but it hides the problem that is difficult to find. When the Printf ("INPUT A:"); this sentence is required to enter a number, then enter a number, then press Enter, the program continues to execute. If we don't wait for the second time, the program is over. There is also the same problem as a GetChar (). Why have this problem? That is because we have made the first time, and press the Enter to make the program continue. And that carriage return will enter the stream. When the second scanf () is encountered, scanf () is received as a second input character. Therefore, the program has also ignored the input and continues to execute. This bus is the garbage character generated during input. When I met it for the first time, I was in writing a cycle. Due to the reason for the spam, the cycle ended early, but destroyed my expectation process (I was depressed when I saw the spam, I was depressed when I saw me) . How can you prevent garbage characters from damaging, or avoid it? And my solution has only two kinds (there are other kinds of words, you must tell me). The first method is to define a character variable, such as CHAR C; Put the variables C to the input. Such as: a = getchar (); c = getChar (); ... b = getchar (); c = getchar (); this can automatically enter the spam into the variable C without destroying our correct processes and inputs. If it is scanf (), you can write: scanf ("% C% C", & A, & C); this effect is the same as getchar (). Such methods are undoubtedly a waste of memory space and add a lot of redundant code. The second method is to use the getChe () function without using scanf () and getchar (). This function is also receiving characters, but it doesn't wait for the input of the carriage return! The characters are sent directly into the stream so that the emergence of spam can be avoided. This is already a long talk about the master, many masters are very depressed (main we have no experience).嘿嘿 ~~~! I hope that the master should not laugh! And what is wrong, I hope the master must point out!