GATE / CS ARCHIVE

GATE CSE 2000 / QUESTION 2.20

Persistent local state across function calls

Programming in C · Scope, Lifetime & Storage Classes · Full GATE CSE 2000 paper

GATE 2000, Q2.20 2 marks · MCQ
Report issue

Scope, Lifetime & Storage Classes

Question 2.20, GATE 2000: Function incr(i) has static int count=0, executes count=count+i and returns count. main loops i=0 through 4 inclusive, assigning j=incr(i). Find final j among 10,4,6,7.
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

Step 1: A static local is initialized once and retains its value between calls. The successive arguments are 0,1,2,3,4.

Step 2: The retained count becomes 0,1,3,6,10. Each returned value is assigned to j, so the final assignment stores 10.

Thus A. Reinitializing count on every call would incorrectly yield 4; stopping the loop before i=4 would yield 6, but the printed condition is inclusive.

Original paper, page 6