Does printf newline have a string buffer?

Language: C, OS: Linux

the code:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main(void)
{
    fork();
    printf("hello world\n");
    fork();
    printf("bye\n");
    return 0;
}

Conclusion:

hello world
bye
hello world
bye
hello world
bye
hello world
bye

In accordance with this , and this , printf() buffers output until a newline is encountered.

So, why do we have 4 "peace hello" in this case? (instead of 2 "hello world")

Edit : Sorry, but, like @GregHewgill, I run this program from an environment where the output cannot be directly connected to the terminal, when I check it again on my computer, it just starts as expected.

+4
source share
1 answer

Accordingly, printf () buffers are output until a new line appears.

, . :

$ ./a.out >out_file

. , .

"" (2 hello world 4 bye) - , setbuf:

setbuf(stdout, 0);

fflush:

fflush(stdout);

printf .

+2

Source: https://habr.com/ru/post/1673334/


All Articles