GATE / CS ARCHIVE

SIMILAR QUESTIONS / PROGRAMMING IN C

Trace static versus automatic state across repeated calls

6 questions solved the same way, newest first. Back to Programming in C

How these are solved

Keep function-local static storage across calls while recreating automatic/register locals and respecting lexical shadowing. Apply calls in their actual control-flow positions and combine returned/printed values.

1 mark · NAT
Report issue

Scope, Lifetime & Storage Classes

Question 35, GATE 2023: In C, funcp has static int x=1, then increments x and returns it. main declares separate int x,y, executes x=funcp(); y=funcp()+x; and prints x+y. What integer is printed?
Tap to enlarge
Answer & solution

Official answer: 7

The static local x in funcp persists across calls. Its first call increments 1 to 2 and returns 2, assigned to main’s x. The second increments the same static value 2 to 3; main’s y becomes 3+2=5. Therefore main prints x+y=2+5=7. The local static and main’s x are distinct objects.

Teaching detail. A static local has block scope but static storage duration: its initialization occurs once, and the stored value survives a return from the function. It is not reinitialized to 1 at each invocation. Track the function's private x separately from main's x to avoid accidental overwriting: after the first call they both happen to equal 2, but the second call changes only the static variable to 3. main's previously stored x remains 2 and participates in both subsequent arithmetic expressions.

Original paper, page 33 · Answer key

2 marks · MCQ
Report issue

Static state and for-loop evaluation order

Scope, Lifetime & Storage Classes

Question 27, GATE 2019: r() contains static int num=7 and returns num--. main executes for(r(); r(); r()) printf("%d",r()); then returns. Which displayed output is correct: A 41, B 52, C 63, D 630?
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Step through it (29 steps)

1. A static local variable is initialized once and keeps its value between calls. Post-decrement returns the old num and then reduces the stored value by one.
2. A for loop evaluates its initializer once, then repeatedly evaluates condition, body, and increment in that order. Calls in the initializer and increment still change num even though their returned values are discarded.
3. The initializer returns 7 and leaves num=6. The first condition returns 6, which is true, and leaves num=5. The body returns and prints 5, leaving num=4. The increment returns 4 and leaves num=3.
4. The second condition returns 3, so the body prints 2 and leaves num=1. The increment returns 1 and leaves num=0. The next condition returns 0, which is false, so there is no third body execution.
5. Only body calls print. No space or newline separates the two digits, so the displayed output is 52, option B. In particular, the zero used to stop the loop is never printed.

Original paper, page 9 · Answer key

2 marks · NAT
Report issue

Scope, Lifetime & Storage Classes

Question 42, GATE 2015: Determine the output of this C program, using the standard function-call sequencing model:
#include <stdio.h>
int f1(void); int f2(void); int f3(void);
int x=10;
int main() { int x=1; x += f1()+f2()+f3()+f2(); printf("%d",x); return 0; }
int f1() { int x=25; x++; return x; }
int f2() { static int x=50; x++; return x; }
int f3() { x *= 10; return x; }
Tap to enlarge
Answer & solution

Official answer: 230

1. There are three distinct variables named x: global x, main’s automatic x, and f2’s persistent static x; f1 additionally creates its own automatic x on entry. Name lookup selects the nearest declaration, so f3 alone modifies the global. The two f2 returns may exchange evaluation order, but their sum stays 103.

2. Main’s local x starts 1. f1 returns 26 from its own automatic x; the two f2 calls return 51 and 52 from one static x in either order; f3 multiplies global 10 by 10 and returns 100. Sum1+26+51+52+100=230. Function executions are indeterminately sequenced, so unspecified operand order does not change the sum or cause overlapping unsequenced increments.

Original paper, page 67 · Answer key

2 marks · MCQ
Report issue

Static local storage and variable shadowing

Scope, Lifetime & Storage Classes

Question 48, GATE 2012: Common code (source-era C syntax retained):
int a,b,c=0;
void prtFun(void);
main() {
 static int a=1; /* Line 1 */
 prtFun(); a+=1; prtFun();
 printf(" \n %d %d ",a,b);
}
void prtFun(void) {
 static int a=2; /* Line 2 */
 int b=1;
 a+=++b;
 printf(" \n %d %d ",a,b);
}
What three output pairs are printed?
(A) (3,1);(4,1);(4,2) (B) (4,2);(6,1);(6,1) (C) (4,2);(6,2);(2,0) (D) (3,1);(5,2);(5,2).
Question 48, GATE 2012: Common code (source-era C syntax retained):
int a,b,c=0;
void prtFun(void);
main() {
 static int a=1; /* Line 1 */
 prtFun(); a+=1; prtFun();
 printf(" \n %d %d ",a,b);
}
void prtFun(void) {
 static int a=2; /* Line 2 */
 int b=1;
 a+=++b;
 printf(" \n %d %d ",a,b);
}
What three output pairs are printed?
(A) (3,1);(4,1);(4,2) (B) (4,2);(6,1);(6,1) (C) (4,2);(6,2);(2,0) (D) (3,1);(5,2);(5,2).
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Assume the snippet is meant as a storage-duration exercise with an appropriate printf declaration and a valid int main(void) supplied; the printed implicit-int main and omitted stdio declaration are not a portable modern-C complete program. The file-scope b has static storage and starts at 0. In prtFun, local b starts at 1 on each call and ++b makes it 2. Its static a starts at 2 once, then becomes 4 on the first call and 6 on the second, printing (4, 2) and (6, 2). The separate main-local a starts at 1 and is incremented to 2; main sees global b=0 and prints (2, 0). Hence C.

Check: There are three different a objects and two different b objects. The function-local a persists across calls but is not main’s a; function-local b shadows global b and never changes it. Keeping an object-by-object trace avoids incorrectly carrying local b back into main.

Original paper, page 13 · Answer key

2 marks · MCQ
Report issue

Automatic and register local lifetimes

Scope, Lifetime & Storage Classes

Question 49, GATE 2012: Common code (source-era C syntax retained):
int a,b,c=0;
void prtFun(void);
main() {
 static int a=1; /* Line 1 */
 prtFun(); a+=1; prtFun();
 printf(" \n %d %d ",a,b);
}
void prtFun(void) {
 static int a=2; /* Line 2 */
 int b=1;
 a+=++b;
 printf(" \n %d %d ",a,b);
}
Replace Line 1 by auto int a=1; and Line 2 by register int a=2;. What three pairs are printed?
(A) (3,1);(4,1);(4,2) (B) (4,2);(6,1);(6,1) (C) (4,2);(6,2);(2,0) (D) (4,2);(4,2);(2,0).
Question 49, GATE 2012: Common code (source-era C syntax retained):
int a,b,c=0;
void prtFun(void);
main() {
 static int a=1; /* Line 1 */
 prtFun(); a+=1; prtFun();
 printf(" \n %d %d ",a,b);
}
void prtFun(void) {
 static int a=2; /* Line 2 */
 int b=1;
 a+=++b;
 printf(" \n %d %d ",a,b);
}
Replace Line 1 by auto int a=1; and Line 2 by register int a=2;. What three pairs are printed?
(A) (3,1);(4,1);(4,2) (B) (4,2);(6,1);(6,1) (C) (4,2);(6,2);(2,0) (D) (4,2);(4,2);(2,0).
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Under the same intended valid-declarations assumptions as the preceding common-data question, changing main-local a to auto does not change its lifetime within this single invocation of main: it still becomes 2. Changing prtFun-local a to register gives it automatic storage duration, not persistent storage; each call initializes it to 2. Each call also initializes local b=1, increments b to 2, adds 2 to a, and prints (4, 2). Finally main prints its a=2 and the zero-initialized global b=0. The three output pairs are (4, 2), (4, 2), (2, 0), option D. register does not guarantee physical register allocation.

Check: Storage-class changes affect persistence, not lexical scope. Both modified declarations still shadow the same globals. The register local is recreated on every invocation, so it cannot retain the first call’s value 4 when the second call starts.

Original paper, page 13 · Answer key

2 marks · MCQ
Report issue

Persistent local state across function calls

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