title: Why a JS function returns different results in C++?
body: <p>A riddle:</p>
  
  <p>You have 3 catalogs. Each of them contains 2 catalogs. Each of those catalogs contains 1 catalog, and those are empty. How many catalogs do you have?</p>
  
  <p>I wrote a JavaScript function to solve it…</p>
  
  <pre><code>function catalogsNumber(c)
  { 
      n = 0;
  
      for (a = 0; a &lt; c; a++) {
          n = (n + 1) * (a + 1);
      }
  
      return n;
  }
  </code></pre>
  
  <p>…and it returns correct results:</p>
  
  <pre><code>// Results in JavaScript
  catalogsNumber ( 3 ) =&gt; 15
  catalogsNumber ( 4 ) =&gt; 64
  catalogsNumber ( 5 ) =&gt; 325
  catalogsNumber ( 12 ) =&gt; 1302061344
  catalogsNumber ( 13 ) =&gt; 16926797485
  catalogsNumber ( 14 ) =&gt; 236975164804
  </code></pre>
  
  <p>But when I tried the same in C++, the function returned strange results every time when <code>c &gt; 12</code>:</p>
  
  <pre><code>int catalogsNumber(int c)
  {
      int i, n = 0;
  
      for (i = 0; i &lt; c; i++) {
          n = (n + 1) * (i + 1);
      }
  
      return n;
  }
  
  // Results in С++
  catalogsNumber ( 3 ) =&gt; 15              // correct
  catalogsNumber ( 4 ) =&gt; 64              // correct
  catalogsNumber ( 5 ) =&gt; 325             // correct
  catalogsNumber ( 12 ) =&gt; 1302061344     // correct
  catalogsNumber ( 13 ) =&gt; -253071699     // ¯\_(ツ)_/¯
  catalogsNumber ( 14 ) =&gt; 751963524      // ¯\_(ツ)_/¯
  </code></pre>
  
  <p>Such a riddle, very mystery. Why does it happen?</p>