日期:2014-05-16  浏览次数:20623 次

为啥死循环外的printf没法输出内容呢?

#include <stdio.h>

int main()
{
        printf("HELLO WOLRD");
        while(1){
        }
}


如上面代码所示,程序可以编译并且执行,但是printf就是没有办法输出,把while死循环去掉就可以了


------解决方案--------------------
printf是带缓冲I/O,那么有while(1)时,程序不flush缓冲。而没有while(1)时,程序结束前,要flush缓冲,将内容输出到终端。

------解决方案--------------------
用fflush(stdin)刷新输入缓冲区,或者输出到stderr:
fprintf(stderr, "HELLO WOLRD");