What will be the output of this C program?
To determine the output of the given C program, let's break down the code step by step:
#include <stdio.h>
int main() {
int i;
for (i = 0; i < 5; i++) {
int i = 10; // This declares a new local variable i inside the loop
printf("%d", i); // Prints the value of the local i
i++; // Increments the local i, but this does not affect the loop counter
}
return 0;
}
Here are the key points:
- The
for
loop runs 5 times, with the loop counteri
ranging from 0 to 4. - Inside the loop, a new local variable
i
is declared and initialized to 10. This locali
shadows the loop counteri
. - The
printf
statement prints the value of this locali
, which is always 10. - The local
i
is incremented, but this does not affect the loop counter.
Given this analysis, the output of the program will be:
10 10 10 10 10
This is because the local variable i
inside the loop is always initialized to 10 and printed, regardless of the loop counter's value4.