清空 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.