Its purpose is to eliminate reading characters that do not conform to the format, such as entering 123xy. In the scanf function, only 123 is read to loop. If there is no while((a=getchar ())! ='\n'), the remaining xy will keep an infinite loop, and the data entered later cannot be read in.