Saturday, 25 February 2017

GATE EXAM PREPARATION IN "C" - (GATE CS 2000)

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);
}



(a) 10
(b) 4
(c) 6
(d) 7
Answer (a)
Explanation: count is static variable in incr(). Statement static int count = 0 will assign count to 0 only in first call. Other calls to this function will take the old values of count.
Count will become 0 after the call incr(0)
Count will become 1 after the call incr(1)
Count will become 3 after the call incr(2)
Count will become 6 after the call incr(3)
Count will become 10 after the call incr(4)

No comments:

Post a Comment