title: Undefined Behaviour of atoi function
body: <p>The following code is showing a strange behaviour :</p>
  
  <pre><code>int main()
  {
      char numArr[] = {'9','8','5'};
      int num;
      printf("%d\n",num);
      num = atoi(numArr);
      printf("%d\n",num);
      return 0;
  }
  </code></pre>
  
  <p>I haven't initialized the <code>num</code> so it contains a garbage value (<code>51</code> in my case) and when I store the return value of <code>atoi()</code> in num the garbage is <strong>overwritten</strong> and it gets the value returned by <code>atoi</code> appended by <code>3</code> (the char for <code>51</code>) so the output after conversion is <code>9853</code> instead of <code>985</code>.</p>
  
  <p>It also shows correct behaviour if I initialize num with any value and no appendant is seen.</p>
  
  <ul>
  <li>If I try it in <a href="http://ideone.com/aSsK1s" rel="nofollow">ideone</a> it gets correct results(because they initialize num automatically with 0), but works undesirably in code::blocks on my machine.</li>
  </ul>
  
  <p>I want to know why the garbage value is being appended to return value ?</p>