title: Difference between variable initialization outside and inside the for loop
body: <p>I have written a program to print out all the numbers, whose binary pattern is symmetrical(for example:1001001 , 1010101) between 1 to 1993. I had made a mistake and I found it out. However, there is some detail I can not understand yet.</p>
  
  <p>The correct code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  main() {
      int binary[11] = {0};
      int i, n, j, k;
      for(i = 1; i &lt;= 1993; i++) {
          n = i; k = 0;
          while(n != 0) {
              binary[k++] = n%2 ; 
              n = n/2;
          }   
          for(j = 0; j &lt; k; j++) {
              if(binary[j] != binary[k-j-1])
              break;
          }   
          if(j == k) {
              printf("%d ", i);
              for(j = 0; j &lt; k; j++)
                  printf("%d", binary[j]);
              printf("\n");
          }   
      }
  }
  </code></pre>
  
  <p>The wrong code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  main() {
      int binary[11] = {0};
      int i, n, j, k = 0; /* I have found a mistake here, k=0 should inside the for loop */     
      for(i = 1; i &lt;= 1993; i++) {
          n = i;
          while(n != 0) {
              binary[k++] = n%2 ; 
              n = n/2;
          }   
          for(j = 0; j &lt; k; j++) {
              if(binary[j] != binary[k-j-1])
              break;
          }   
          if(j == k) {
              printf("%d ", i);
              for(j = 0; j &lt; k; j++)
                  printf("%d", binary[j]);
              printf("\n");
          }   
      }
  }
  </code></pre>
  
  <p>I just can't understand why the variable "i" can not increase to 6 when "k=0" outside the for loop, and the loop will repeat again and again.</p>