int main() {
int j = 0;
int i = 0;
for (j = 0; j < 5; j++) {
printf("Iteration %d : %d ", j + 1, i);
int i;
printf("%d", i);
i = 5;
printf("\n");
}
}
The above code generates the following output:
Iteration 1 : 0 0
Iteration 2 : 0 5
Iteration 3 : 0 5
Iteration 4 : 0 5
Iteration 5 : 0 5
I'm not able to understand why the second printf
value in iterations 2,3,4,5
is 5.
My understanding of why the first value is 0 in each iteration is that the scope of i
in the for
loop is local and it is destroyed as soon as we go into a new iteration as i
was declared in the for
loop.
But I'm not able to figure out why this value becomes 5 at the second printf
.