title: C++ integer overflow
body: <p>I'm just starting to teach myself C++, and have begun learning about integer overflow. Out of curiosity I wrote some tests just to see what occurs with certain integer values.</p>
  
  <p>Here's my program:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  int main()
  {
      int x(0);
      std::cout &lt;&lt; x &lt;&lt; std::endl;
  
      x = x + 2147483647;
      std::cout &lt;&lt; x &lt;&lt; std::endl;
  
      x = x + 1;
      std::cout &lt;&lt; x &lt;&lt; std::endl;
      std::cout &lt;&lt; std::endl;
  
      unsigned int y(0);
      std::cout &lt;&lt; y &lt;&lt; std::endl;
  
      y = y + 4294967295;
      std::cout &lt;&lt; y &lt;&lt; std::endl;
  
      y = y + 1;
      std::cout &lt;&lt; y &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>And here's the output:</p>
  
  <pre><code>0
  2147483647
  -2147483648
  
  0
  4294967295
  0
  </code></pre>
  
  <p>The output surprised me a bit, and I'm wondering if someone could explain why this happened, OR if the unexpected-ness of these results is expected; so this may just be the result of my specific machine.</p>