Skip to main content
Perto56
Associate
February 2, 2021
Solved

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

  • February 2, 2021
  • 3 replies
  • 1567 views

Works:

  printf("hello world\n");

Does not work:

  printf("goodbye world !");

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

    This topic has been closed for replies.
    Best answer by Uwe Bonnes

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

    3 replies

    Uwe Bonnes
    Uwe BonnesBest answer
    Chief
    February 2, 2021

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

    KnarfB
    Super User
    February 2, 2021

    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
    Perto56Author
    Associate
    February 2, 2021

    Thanks alot.