title: Why is stdout buffering?
body: <p>I am trying to learn the <code>libuv</code> api and wrote the following test:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;uv.h&gt;
  
  void timer_cb(uv_timer_t* timer) {
      int* i = timer-&gt;data;
      --*i;
      if(*i == 0) {
         uv_timer_stop(timer);
      }
      printf("timer %d\n", *i);
      //fflush(stdout);
  }
  
  int main() {
      uv_loop_t* loop = uv_default_loop();
      uv_timer_t* timer = malloc(sizeof(uv_timer_t));
      uv_timer_init(loop, timer);
      int i = 5;
      timer-&gt;data = &amp;i;
      uv_timer_start(timer, timer_cb, 1000, 2000);
  
      uv_run(loop, UV_RUN_DEFAULT);
  
      printf("Now quitting.\n");
      uv_close(timer, 0);
      uv_loop_close(loop);
  
      return 0;
  }
  </code></pre>
  
  <p>When  run it, no output is displayed until the program finishes running, and then all the output is displayed at once. If I uncomment the <code>fflush</code> line it works as expected, writing every 2 seconds.</p>
  
  <p>Can someone please explain this to me? Why is <code>stdout</code> not flushed after the newline, as is explained <a href="https://stackoverflow.com/a/1716621/331785">here</a> and in other places? Why do I need tomanually flush it?</p>