8,070 views

The value of $j$ 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:

1. $10$
2. $4$
3. $6$
4. $7$

• At $i=0, j=0$
• At $i=1, j=1$
• At $i=2, j=3$
• At $i=3, j=6$
• At $i=4, j=10$

$\frac{(n)*(n+1)}{2}$ Put n = 4.
This is because count being a static variable is not reinitialized at every call post the first call of incr(i)
A static local variable is a place to hide data form other functions but retain it for future calls of the same function.