title: printf anomaly after "fork()"
body: <p>OS: Linux, Language: pure C</p>
  
  <p>I'm moving forward in learning C programming in general, and C programming under UNIX in a special case.</p>
  
  <p>I detected a strange (for me) behaviour of the <code>printf()</code> function after using a <code>fork()</code> call. </p>
  
  <p><strong>Code</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;system.h&gt;
  
  int main()
  {
      int pid;
      printf( "Hello, my pid is %d", getpid() );
  
      pid = fork();
      if( pid == 0 )
      {
              printf( "\nI was forked! :D" );
              sleep( 3 );
      }
      else
      {
              waitpid( pid, NULL, 0 );
              printf( "\n%d was forked!", pid );
      }
      return 0;
  }
  </code></pre>
  
  <p><strong>Output</strong></p>
  
  <pre><code>Hello, my pid is 1111
  I was forked! :DHello, my pid is 1111
  2222 was forked!
  </code></pre>
  
  <p>Why did the second "Hello" string occur in the child's output?</p>
  
  <p>Yes, it is exactly what the parent printed when it started, with the parent's <code>pid</code>.</p>
  
  <p>But! If we place a <code>\n</code> character at the end of each string we get the expected output:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;system.h&gt;
  
  int main()
  {
      int pid;
      printf( "Hello, my pid is %d\n", getpid() ); // SIC!!
  
      pid = fork();
      if( pid == 0 )
      {
              printf( "I was forked! :D" ); // removed the '\n', no matter
              sleep( 3 );
      }
      else
      {
              waitpid( pid, NULL, 0 );
              printf( "\n%d was forked!", pid );
      }
      return 0;
  }
  </code></pre>
  
  <p><strong>Output</strong>:</p>
  
  <pre><code>Hello, my pid is 1111
  I was forked! :D
  2222 was forked!
  </code></pre>
  
  <p>Why does it happen? Is it correct behaviour, or is it a bug?</p>