printf without \n does not display text when placed before while(1) [duplicate]
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.
精彩评论