title: Why does Rust code using printf in a loop from C not display output, but using std::cout in a loop from C++ does?
body: <p>I use Rust to call a C function with a loop (<code>for</code> or <code>while</code>), which uses <code>printf</code> function to display something, but the C function doesn't display any output from <code>printf</code>.</p>
  
  <p>C code:</p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  
  void print_c() {
    int s = 3;
    while (1) {
      printf("%d\n", s);
      sleep(1);
    }
  }
  </code></pre>
  
  <p>However, when using C++, output is successfully generated.</p>
  
  <p>C++ code:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;stdint.h&gt;
  #include &lt;unistd.h&gt;
  
  extern "C" {
    int print_it(int32_t num) {
      while (1) {
          std::cout &lt;&lt; num &lt;&lt; std::endl;
          sleep(1);
      }
    }
  }
  </code></pre>
  
  <p>C is packaged as a shared library named "cthread". C++ is named "cppthread".</p>
  
  <p>This is the Rust calling the C library:</p>
  
  <pre class="lang-rust prettyprint-override"><code>#[link(name = "cthread")]
  extern "C" {
      fn print_c();
  }
  
  fn main() {
      unsafe { print_c() };
  }
  </code></pre>
  
  <p>And the Rust calling the C++ library:</p>
  
  <pre class="lang-rust prettyprint-override"><code>#[link(name = "cppthread")]
  extern "C" {
      fn print_it();
  }
  
  fn main() {
      unsafe { print_it() };
  }
  </code></pre>