The GATE Grind

GATE 2015 CS (CS3) – Question 42

Programming and Data Structures · Programming in C · 2 marks · Numerical answer

What is printed?

#include <stdio.h>
int f1(void); int f2(void); int f3(void);
int x=10;
int main(void) {
 int x=1;
 x += f1()+f2()+f3()+f2();
 printf("%d",x); return 0;
}
int f1(void) { int x=25; x++; return x; }
int f2(void) { static int x=50; x++; return x; }
int f3(void) { x*=10; return x; }

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: 230

Explanation

Main’s local x starts at one. f1 returns twenty-six from its independent local variable. The two f2 calls return fifty-one and fifty-two because its static variable persists, regardless of their order. f3 multiplies the global ten by ten and returns one hundred. Thus main computes 1+26+51+100+52=230.

More Programming in C questions

All Programming in C questions →