+ 1
Explain the Output
#include <stdio.h> int main() { int i; for ( i=0; i<5; i++ ) { int i = 10; printf ( "%d ", i ); i++; } return 0; } Here answer is 5 times 10 can anyone explain how?
1 Answer
+ 2
the scope of the inner i(int i = 10) is only within the for loop and has got nothing to do with the for loop initialize, condition and increment part.
Why five 10 as output?
so basically after we print i inside the for loop, i is incremented but notice in the next iteration again we are assigning i with 10 (this line i = 10).
hence we have 10 10 10 10 10.
do read and play with scope of variable to know what's going on behind the scene.