为什么死循环前面不能输出??????
#include <stdio.h>
int main()
{
printf( "haha ");
while(1)
{
;
}
return 0;
}
执行时haha根本没有被输出.为什么.请大侠们指教.
[解决办法]
缓冲有三种类型:无缓冲、行缓冲以及全缓冲。
C 标准对程序开始执行时的标准输入(stdin)、标准输出(stdout)和标准错误输出(stderr)使用缓冲的情况作了如下规定:
As initially opened, the standard error stream is not fully buffered; the standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device.
因此,在程序执行开始的时候(或者说默认情况下,即没有人为改变其缓冲方式的情况下):
stderr 可能是无缓冲、行缓冲,但不能是全缓冲。
stdin 和 stdout 可能是无缓冲、行缓冲,也可能是全缓冲。不过,stdin 和 stdout 如果分别是指键盘和显示器等交互设备(interactive device)的话,那么只能是无缓冲或行缓冲。
至于它们到底采取何种缓冲方式则是实现相关的。只是在一般的计算机系统上,stderr 无缓冲、stdin 和 stdout 行缓冲的情况比较多见。在不违背标准规定的情况下,各个系统可以选择适合自己的缓冲方式。