Skip to content
Official PYQ · 2015

GATE CS 2015 Set 3 Q54 — Programming in C

Explain with AI

Consider the following C program:

#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;}

The output of the program is ______.

View worked solution

Answer: 230

To find the output of the program, we trace the evaluation of the expression in main():

x+=f1()+f2()+f3()+f2();\text{x} += f1() + f2() + f3() + f2();

Let's analyze each function call step-by-step:

  1. f1():
    • Declares a local variable x = 25.
    • Increments it to 26 and returns 26.
  2. First call to f2():
    • Uses a static variable x initialized to 50.
    • Increments it to 51 and returns 51.
  3. f3():
    • Accesses the global variable x (initially 10).
    • Updates global x to 10×10=10010 \times 10 = 100 and returns 100.
  4. Second call to f2():
    • The static variable x (currently 51) is incremented to 52 and returns 52.

Now, we sum the returned values:

Sum=26+51+100+52=229\text{Sum} = 26 + 51 + 100 + 52 = 229

In main(), the local variable x is initially 1. The assignment operator updates it as:

x+=229  ⟹  x=1+229=230x += 229 \implies x = 1 + 229 = 230

Thus, the program prints 230.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Programming in C questions