Skip to content
Official PYQ · 2023

GATE CS 2023 Q25 — Programming in C

Explain with AI

The integer value printed by the ANSI-C\textsf{ANSI-C} program given below is _______________

#include<stdio.h>

int funcp(){
    static int x = 1;
    x++;
    return x;
}

int main(){
    int x,y;
    x = funcp();
    y = funcp()+x;
    printf("%d\n", (x+y));
    return 0;
}
View worked solution

Answer: 7

To find the output of the program, let's trace the execution step-by-step. Note that the variable x inside funcp() is declared as static, meaning it retains its value across multiple function calls and is initialized only once.

Step-by-Step Execution:

  1. First call to funcp():
    • The static variable funcp::x is initialized to 11 (this initialization happens only once).
    • x++ increments funcp::x to 22.
    • The function returns 22.
    • In main(), the local variable x is assigned this returned value: main::x=2\text{main::x} = 2
  2. Second call to funcp():
    • The static variable funcp::x retains its previous value of 22.
    • x++ increments funcp::x to 33.
    • The function returns 33.
  3. Evaluating y = funcp() + x in main():
    • The call funcp() returns 33.
    • The local variable main::x is 22.
    • Thus, main::y=3+2=5\text{main::y} = 3 + 2 = 5
  4. Printing the result:
    • The program prints the value of x + y: main::x+main::y=2+5=7\text{main::x} + \text{main::y} = 2 + 5 = 7

Therefore, the integer value printed by the program is 7.

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

Published
Report a correction
Keep exploringMore Programming in C questions