fflush(stdin)在C编程中有什么用? [英] What is the use of fflush(stdin) in c programming?

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

问题描述

我有以下程序

#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(stdin)的用途是什么?

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

推荐答案

它不在标准C语言中,因此行为未定义.

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

某些实现使用它来清除 stdin 缓冲区.

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.

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

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