GATE 2012 CS (CS1) – Question 58
What are the three output lines?
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);
}Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: (C) (4,2); (6,2); (2,0)
Explanation
prtFun’s static a begins at two and persists. Each call creates local b=1, increments it to two and adds two to a, printing (4,2) then (6,2). main has its own static a, changing from one to two; its b resolves to the zero-initialized global. The final line is (2,0). Thus C.