The GATE Grind

GATE 2023 CS – Question 35

Programming and Data Structures · Programming in C · 1 mark · Numerical answer

The integer value printed by the 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;
}

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: 7

Explanation

`static int x = 1;` inside `funcp` is created once and keeps its value between calls (the `x` in `main` is a different variable).

The program prints $x+y=2+5=\mathbf7$.

More Programming in C questions

All Programming in C questions →