首页 > 其他分享 >c语言中printf不输出任何东西?,缓冲区未满不输出任何东西

c语言中printf不输出任何东西?,缓冲区未满不输出任何东西

时间:2023-02-19 13:44:34浏览次数:27  
标签:输出 stdout int printf 缓冲区 include

下面代码为什么没有任何输出:

#include<cstdio>
#include<unistd.h>
int main(int argc, char **argv){
        while(1){
                sleep(1);
                printf("fffffff");
        }
        return 0;
}

答:由于标准输出流(stdout)通常是行缓冲的,这意味着输出将在打印完整行之前被缓冲起来。因此,即使程序正在打印 "fffffff",输出可能不会立即出现。为了确保程序的输出被立即显示,您可以在 printf() 函数后添加 fflush(stdout) 来强制刷新输出缓冲区,代码修改如下:

#include <cstdio>
#include <unistd.h>

int main(int argc, char **argv){
    while (1){
        sleep(1);
        printf("fffffff");
        fflush(stdout); // 刷新标准输出流缓冲区
    }
    return 0;
}

标签:输出,stdout,int,printf,缓冲区,include
From: https://www.cnblogs.com/codingbigdog/p/17134629.html

相关文章