title: Array index loop variable resets to zero in C, C++
body: <p>I have declared an array of length N (suppose). Now, I assign some values to the elements of this array using a loop (the loop variable is used as the array's index). The code's as follows:</p>
  
  <pre><code>int main()
  {
    int arr[4], j;
    for(j=0; j&lt;10; j++)
    {     
      printf("%d\n", j);
      arr[j] = 0;
      sleep(1);
      printf("%d\n\n", j);
    } 
  return 0;
  }
  </code></pre>
  
  <p>I expected the output to be 0 1 2 .. 9. But what actually happened was that j got reset to 0 when the assignment arr[N+2]=0 (arr[6]=0, in this case) was executed in the loop.
  What's actually happening here? Am I missing something?</p>