Skip to main content

GATE CSE 2015Programming in C › Functions & Parameter PassingVerified

GATE CSE 2015 PYQ: Consider the following C program: #include <stdio.h> int f1 ( void ); int f2 ( void ); int f3 ( void ); int…

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 ______.

    Answer

    230

    Approach

    Trace the scope and lifetime of each variable, evaluate the four function calls in the expression, and then apply the compound assignment to main's local variable.

    The program prints 230. The local static variable in f2 retains its value between the two calls, giving 51 and 52, while f3 modifies the global x to return 100. The final addition is 1 + 26 + 51 + 100 + 52 = 230.

    Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.