title: C++: Why does std::system("exit 1") return 256?
body: <p>I want to run a little UNIX shell script inside my C++ program and I want to capture the exit code of the shell script. But the value returned by std::system is not what I expect it to be:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  
  int main()
  {
    std::cout &lt;&lt; std::system("echo Hello &gt;/dev/null") &lt;&lt; std::endl;
    std::cout &lt;&lt; std::system("which does_not_exisit 2&gt;/dev/null") &lt;&lt; std::endl;
  
    std::cout &lt;&lt; std::system("exit 0") &lt;&lt; std::endl;
    std::cout &lt;&lt; std::system("exit 1") &lt;&lt; std::endl;
    std::cout &lt;&lt; std::system("exit 2") &lt;&lt; std::endl;
    std::cout &lt;&lt; std::system("exit 3") &lt;&lt; std::endl;
    std::cout &lt;&lt; std::system("echo exit 4 | bash") &lt;&lt; std::endl;
  
    return 0;
  }
  </code></pre>
  
  <p>On my Linux box this produces:</p>
  
  <pre><code>0
  256
  0
  256
  512
  768
  1024
  </code></pre>
  
  <p>It seems like all exit codes greater than 0 are multiplied by 256. What's the reason for this behavior? Is this portable across UNIX like operating systems?</p>