title: Difference in Output between printf("\n...") and printf("..\n") in Linux
body: <p>Can somebody explain to me why these two similar codes (except for the <code>\n</code> position) result in different output:</p>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;stdio.h&gt;
  
  int main()
  {
      int pid, i=0;
      printf("Ready to fork\n");
      pid=fork();
      if (pid==0)
      {
          printf("Child starts\n");
          for (i=0; i&lt;1000; i++);
          printf("Child ends\n");
      }
      else
      {
          wait(0);
          for (i=0; i&lt;1000; i++);
          printf("Parent process ends\n");
      }
      return 1;
  }
  </code></pre>
  
  <p>Output: </p>
  
  <p><a href="https://i.stack.imgur.com/CjsPz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CjsPz.png" alt="enter image description here"></a></p>
  
  <p>And this:</p>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include&lt;sys/wait.h&gt;
  #include &lt;stdio.h&gt;
  
  int main()
  {
      int pid, i=0;
      printf("\nReady to fork %d", getpid());
      pid=fork();
      if (pid==0)
      {
          printf("\nChild starts %d",getpid());
          for (i=0; i&lt;1000; i++);
          printf("\nChild ends %d", getpid());
      }
      else
      {
          wait(0);
          for (i=0; i&lt;1000; i++);
          printf("\nParent process ends %d", getpid());
      }
      return 1;
  }
  </code></pre>
  
  <p>Results in:</p>
  
  <p><a href="https://i.stack.imgur.com/xdZWs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xdZWs.png" alt="enter image description here"></a></p>
  
  <p>I really can’t find any satisfactory reason of why a simple change in the position of <code>\n</code> changes the output of the program at the level where the parent program seems to restart after fork execution completes.</p>
  
  <p>Thank you in advance.</p>