int incr(int i){ static int count = 0 : GATE 2000

 

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);
	printf("%d",j);
}

What is the output?

(a) 10    (b) 4    (c) 6    (d) 7

Answer

Output is (a) 10

The function incr is called total 5 times with increasing values of i. But, as count is a static variable, its value will persist between different function calls. So, following will be the sequence of values.

i:               0    1    2    3    4
count:    0    1    3    6    10
j:               0    1    3    6    10

Therefore, after completion of the for loop, value of j is 10.

0Comment