#include <stdio.h>
#include <unistd.h>
int main()
{
while(1)
{
fprintf(stdout,"hello-out");
fprintf(stderr,"hello-err");
sleep(1);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我机器上面的输出是
hello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-err
Run Code Online (Sandbox Code Playgroud)
我不得不杀死程序来阻止它.这是正确和预期的行为.或者这是错误的.这是一个面试问题,因此我在这里发帖.
这是预期的输出:
while (1)循环.stdout是行缓冲的(默认情况下),因此只有在'\n'打印换行符()时才会刷新到控制台.由于您不打印任何换行符,因此您永远不会看到任何hello-out文字.stderr不是行缓冲(默认情况下),因此它会在每次新fprintf()调用时更新控制台.