没有 \n 的 printf 放置在 while(1) 之前不显示文本 [英] printf without \n does not display text when placed before while(1)

查看:50
本文介绍了没有 \n 的 printf 放置在 while(1) 之前不显示文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
为什么 printf 在调用后不会刷新,除非格式字符串中有换行符?(在 C 中)

我在做网络项目时遇到了这个问题.我能够缩小问题的范围并像这样重现它:

I faced this problem while doing a networking project. I was able to narrow down the issue and reproduce it like this:

如果您运行此代码,它不会在屏幕上显示文本.尽管如果您将 \n 放在文本末尾或在 printf 语句之后使用 fflush() ,它会显示文本.

If you run this code, it wont display the text on the screen. Although it displays the text if you put \n at the end of the text or use fflush() after the printf statement.

int main(){
printf("started") ;
while(1){
}
}

谁能解释一下这种行为?

Can anyone please explain this behavior?

推荐答案

如果没有 \n,输出就不会刷新到屏幕上.

The output just doesn't get flushed to the screen without the \n.

printf 后面添加 fflush(stdout);,你应该会看到输出.

Add fflush(stdout); after the printf and you should see the output.

这篇关于没有 \n 的 printf 放置在 while(1) 之前不显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆