是什么在C编程中使用fflush的(标准输入) [英] what is the use of fflush(stdin) in c programming

查看:124
本文介绍了是什么在C编程中使用fflush的(标准输入)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下程序

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

int main()
{
   char ans[8];
    int i;
    for(i=1;i<=3;i++)
    {
        printf("\n What is the unit of traffic ?");
        scanf("%s",ans);
        fflush(stdin);

        if(stricmp(ans,"Earlang")==0)
        {
            printf("\nAnswer is correct");
            exit(1);
        }
        else
            if(i<3)
            printf("\n Try Again!\n");
    }
    printf("\n Nunit of traffic is Earlang:");
}

什么是在这个程序中使用fflush的(标准输入)?

What is the use of fflush(stdin) in this program?

推荐答案

这不是标准的C,这样的行为是不确定的。

It's not in standard C, so the behavior is undefined.

一些实现使用它来清除标准输入缓冲区。

Some implementation uses it to clear stdin buffer.

从C11 7.21.5.2的 fflush函数 fflush 仅适用于输出/更新流,而不是输入流。

From C11 7.21.5.2 The fflush function, fflush works only with output/update stream, not input stream.

如果流指向一个输出流或更新流在其中最近
  操作未输入,fflush函数使该流的任何未写入的数据
  被传递到要写入到文件的主机环境;否则,该行为是
  不确定的。

If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

这篇关于是什么在C编程中使用fflush的(标准输入)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆