+ 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?

18th Sep 2020, 3:04 PM
Samir Singh
Samir Singh - avatar
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.
18th Sep 2020, 3:38 PM
minirkk
minirkk - avatar