Skip to main content
Perto56
Associate
Feb 2, 2021 8:57 AM UTC
Solved

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

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.
    Uwe Bonnes
    Uwe BonnesBest answer
    Chief
    Feb 2, 2021 9:25 AM UTC

    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
    Feb 2, 2021 9:25 AM UTC

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

    KnarfB
    Super User
    Feb 2, 2021 9:33 AM UTC

    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
    Feb 2, 2021 3:17 PM UTC

    Thanks alot.