How to clear input in C language?
In the C language, you can use the fflush(stdin) function to clear the input content. This function will clear all contents in the input buffer, including previous inputs and newline characters. You can place this function in the position where you need to clear the input content to empty the input buffer.
For example:
#include <stdio.h>
int main() {
int num;
printf("请输入一个整数:");
scanf("%d", &num);
// 清除输入缓冲区中的内容
fflush(stdin);
printf("输入的整数是:%d\n", num);
return 0;
}
In the example above, the fflush(stdin) function clears the contents of the input buffer to prevent previous input from affecting subsequent input.