Language: C, OS: Linux
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;
}
Output:
hello world
bye
hello world
bye
hello world
bye
hello world
bye
According to this and this, printf() buffers output until a newline is encountered
.
So why does we have 4 "hello world" in this case? (instead of 2 "hello world")
Edit: Sorry all, but like @GregHewgill said, I running this program from an environment where the output cannot be directly to the terminal, when I check it again on my computer, it just run as expected.