清空 C/C++ 中的输入缓冲区


fflush(stdin) 函数用于刷新或清除流的输出缓冲区。当它在 scanf() 后使用时,它也会刷新输入缓冲区。如果成功,它返回零;否则返回 EOF,并且设置 feof 错误指示器。

下面是 fflush(stdin) 在 C 语言中清除输入缓冲区的语法,

int fflush(FILE *stream);

下面是 fflush(stdin) 在 C 语言中清除输入缓冲区的示例,

示例

 实时演示

#include <stdio.h>
#include<stdlib.h>

int main() {
   char s[20];

   printf("Enter the string : \n", s);
   scanf("%s\n", s);
   printf("The entered string : %s", s);
     
   fflush(stdin);
   return 0;
}

输出

下面是输出

Enter the string : helloworld
The entered string : helloworld

更新于: 25-6-2020

24K+ 浏览次数

开启你的 职业生涯

通过完成课程获得认证

立即开始
广告
© . All rights reserved.