Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

printf

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?

like image 349
Aaveg Mittal Avatar asked May 03 '26 14:05

Aaveg Mittal


1 Answers

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.

like image 170
aioobe Avatar answered May 05 '26 03:05

aioobe