The GATE Grind

GATE 2012 CS (CS1) – Question 58

Programming and Data Structures · Programming in C · 2 marks · Multiple choice

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);
}
  1. (3,1); (4,1); (4,2)
  2. (4,2); (6,1); (6,1)
  3. (4,2); (6,2); (2,0)
  4. (3,1); (5,2); (5,2)

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.

More Programming in C questions

All Programming in C questions →