title: Results of printf() and system() are in the wrong order when output is redirected to a file
body: <p>I have a C program that compiles to an executable called myprogram. This is its main function:</p>
  
  <pre><code>int main(int argc, char ** argv) {
    printf("this is a test message.\n");
    system("ls");
  
    return 0;
  }
  </code></pre>
  
  <p>When I run <code>myprogram &gt; output.txt</code> in a Linux shell and then examine output.txt, I see the output of <code>ls</code> listed above "this is a test message." </p>
  
  <p>I feel like it should be the other way around. Why is this happening, and what can I do so that "this is a test message" appears at the top of output.txt?  </p>
  
  <p>If it matters, I'm new to both C and working in a command line.</p>