cancel
Showing results for 
Search instead for 
Did you mean: 

Why does a call to printf need to be terminated by a newline ?

Perto56
Associate

Works:

  printf("hello world\n");

Does not work:

  printf("goodbye world !");

I have tried to find the answer in gcc´without success.

1 ACCEPTED SOLUTION

Accepted Solutions
Uwe Bonnes
Principal III

That is a standard properity of stdout, it is buffered. Use stderr and output is immediate.Or fflush stdout.

View solution in original post

3 REPLIES 3
Uwe Bonnes
Principal III

That is a standard properity of stdout, it is buffered. Use stderr and output is immediate.Or fflush stdout.

KnarfB
Principal III

It doesn't need to. But, by default printf is line buffered. This allows more efficient output if you override _write (instead of __io_putchar). It is also for historical reasons/compatability.

If you want to turn it off:

setvbuf(stdout, NULL, _IONBF, 0);

hth

KnarfB

Perto56
Associate

Thanks alot.