开发者

printf without \n does not display text when placed before while(1) [duplicate]

开发者 https://www.devze.com 2023-04-03 23:50 出处:网络
This questi开发者_运维技巧on already has answers here: Closed 11 years ago. Possible Duplicate: Why does printf not flush after the call unless a newline is in the format string? (in C)
This questi开发者_运维技巧on already has answers here: Closed 11 years ago.

Possible Duplicate:

Why does printf not flush after the call unless a newline is in the format string? (in C)

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

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?


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号