0
#include<stdio.h>
#include<Unistd.h>
Void main()
  { Printf("hi");
    fork();
    Printf("hello");
   }

Output: hi hello hi hello "But when iam using \n in the printf statements iam getting the output as"

Output: hi
        hello
        hello 

[But \n is used for displaying statement in new line but why am i getting this difference] Thank you ...

Hafiza
  • 11
  • 1

1 Answers1

1

printf buffers its output, and when you fork, now the output is in both programs' buffers, so they'll both eventually print it. To fix it, you should force the buffer to be flushed immediately before forking by doing fflush(stdout). You don't see the problem with \n because using \n also flushes the buffer.