fputs和循环的奇怪行为

Jon*_*han 2 c c++ gcc loops stdout

运行以下代码时,我没有输出,但我无法解决原因.

# include <stdio.h>

int main()
{
    fputs("hello", stdout);

    while (1);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果没有while循环,它可以完美地工作,但是只要我添加它就没有输出.当然它应该在开始循环之前输出?它只是在我的系统上吗?我是否需要冲洗某种缓冲区或什么?

提前致谢.

Die*_*Epp 6

你必须冲洗stdout.当您编写换行符时,会自动发生这种情况.更改fputs为:

fputs("hello\n", stdout);
Run Code Online (Sandbox Code Playgroud)

或者:

fputs("hello", stdout);
fflush(stdout);
Run Code Online (Sandbox Code Playgroud)