首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么在使用fprintf时不打印到stdout?

为什么在使用fprintf时不打印到stdout?
EN

Stack Overflow用户
提问于 2012-07-04 00:01:38
回答 2查看 7.4K关注 0票数 16

我有这样的代码:

代码语言:javascript
复制
#include <stdio.h>
#include <unistd.h>
int main()
{
        while(1)
        {
                fprintf(stdout,"hello-out");
                fprintf(stderr,"hello-err");
                sleep(1);
        }
        return 0;
}

输出为hello-err hello-err hello-err hello-err hello-err hello-err,间隔为1秒。我想知道为什么hello-out从不打印出来。

EN

Stack Overflow用户

回答已采纳

发布于 2012-07-04 00:02:27

您需要对stdout进行fflush,因为通常stdout是行缓冲的,并且您不会在程序中发出新的行字符。

代码语言:javascript
复制
            fprintf(stdout,"hello-out");
            fflush(stdout);

默认情况下,stderr不是完全缓冲的,因此您不需要对其进行fflush

票数 15
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11314825

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档