当我运行下面的程序时,我没有得到任何输出。
#include <stdio.h>
int main()
{
printf("hello");
while(1)
{
}
return 0;
}
如果我编辑printf命令,在字符串末尾添加'\n'字符,那么输出结果就是预期的。但第一段代码中发生了什么?我无法理解。
当我运行下面的程序时,我没有得到任何输出。
#include <stdio.h>
int main()
{
printf("hello");
while(1)
{
}
return 0;
}
如果我编辑printf命令,在字符串末尾添加'\n'字符,那么输出结果就是预期的。但第一段代码中发生了什么?我无法理解。
fflush(stdout);
强制刷新缓冲区到终端。顺便说一下,不要尝试刷新stdin
,那是不允许的。尝试
printf("hello\n");
printf("hello");
fflush(stdout)
fflush(stdout)
出现在 printf
之后。 - lurker
fflush(stdout)
,顺便提一下你有一个无限循环。 - Grijesh Chauhan\n
才能打印出来,或者像@GrijeshChauhan建议的那样,在你的printf后使用fflush(stdout)
。还有其他可以刷新输出缓冲区的操作,例如输入函数(getchar
或scanf
),但从你的代码片段中不清楚你的循环正在做什么。 - lurker