StreakPeaked· Practice

ExamsGATETechnical

Consider the following C code segment. int a, b, c = 0; void prtFun(void); main() { static int a = 1; /* Line 1 */ prtFun(); a += 1; prtFun(); printf(" %d %d ", a, b); } void prtFun(void) { static int a = 2; /* Line 2 */ int b = 1; a += ++b; printf(" %d %d ", a, b); } What output will be generated by the given code segment?

  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

Correct answer: 4 2 6 2 2 0

Solution

In prtFun, static a starts 2: first call ++b=2, a=4 -> '4 2'; second call a=6 -> '6 2'. In main the local static a=1 becomes 2 after a+=1, and global b is still 0, printing '2 0'. Output is 4 2 / 6 2 / 2 0.

Related GATE Technical questions

⚔️ Practice GATE Technical free + battle 1v1 →