why these codes are showing different results :
for (int i = 0; i < 5; ++i)
{
static int n = 0;
n++;
cout<<n<<endl; // prints 1 2 3 4 5
}
for (int i = 0; i < 5; ++i)
{
int n = 0;
n++;
cout<<n<<endl; // prints 1 1 1 1 1
}
if static variable n is declared again then, why it is retaining its previous value. What exactly is happening with the scope of static variable inside "for" loop ? Why does not compiler showed error for declaring again an existing variable, during second iteration in the (first)loop ?
Also please tell why these codes are showing different result :
for(i=0;i<5;i++)
{
static int n;
n=0;
n++;
cout<<n<<endl; //Print 1 1 1 1 1
}
for(i=0;i<5;i++)
{
static int n=0;
n++;
cout<<n<<endl; //Print 1 2 3 4 5
}