The value of at the end of the execution of the following C program:
int incr (int i)
{
static int count = 0;
count = count + i;
return (count);
}
main () {
int i, j;
for (i = 0; i <= 4; i++)
j = incr (i);
}
is:
View worked solution
Answer: A
The variable count inside the function incr is declared with the static storage class. In C, a static local variable:
- Is initialized only once (at the beginning of program execution).
- Retains its value between successive function calls.
Let's trace the execution of the for loop in main(), which runs for :
| Iteration | Value of | Previous count | New count = count + i | Returned Value () |
|---|---|---|---|---|
| 1 | (initial) | |||
| 2 | ||||
| 3 | ||||
| 4 | ||||
| 5 |
After the fifth iteration, becomes , the loop condition i <= 4 becomes false, and the loop terminates.
Thus, the final value of at the end of execution is 10.
Free practice · Your answers on this page are not saved.
