Variable Scope in C programming

Viewed 528

In c, when this block of code is run,it outputs 10 10 10 10 10. I think the loop should exit after 1st execution as i becomes 11 but it is not happening. Why it is so?

#include<stdio.h>
int main()
{
  int i; 
  for(i=0;i<5;i++)
  {
     int i=10;  
     printf("%d\t",i);
     i++;
  }
  return;
}

But when program is written as below the output is similar to what i am thinking(i.e.10 ).What is the exact difference between above code and the code shown below? How C is handling these variable? I would be glad if anyone explain about this.

#include<stdio.h>
int main()
{
  int i; 
  for(i=0;i<5;i++)
  {
     i=10;  
     printf("%d\t",i);
     i++;
  }
  return;
}
4 Answers
Related