为什么这个没有先打印就挂起来了

Why does this hang without first printing?

本文关键字:打印 挂起 来了 为什么      更新时间:2023-10-16

为什么在没有第一次打印的情况下挂起?

#include <stdio.h>
void main() {
    printf("hello world");
    while (1) {}
}

因为您还没有刷新标准输出。尝试fflush。更好的是,对于C++的使用。。。

std::cout << "hello world" << std::endl;

另外,如果您添加了n,那么它会有更好的机会自行刷新,但并非所有实现都遵循标准。

printf之后调用fflush(stdout);

这将强制刷新和打印stdout缓冲区。