-1

A static integer variable is declared inside a function. The variable increments by 1 when the function is called. The function is called and returns 3 times. What is the value of the variable on the third function call and what is the value when it returns?

Gurpreet
  • 3
  • 1

2 Answers2

0

When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member. A static member is shared by all objects of the class.

so it will return 3 if the its value start with 0

Abhinav777
  • 41
  • 7
0
int foo() {
   static int x = 0;
   x++;
   return x;
}

The keyword static acts to extend the lifetime of a variable to the lifetime of the program; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().

Shravan40
  • 8,922
  • 6
  • 28
  • 48