2

Below is a output question.I am not able to understand why its answer is 30.

#include<iostream>
using namespace std;                     //namespace std is being used                      

int &fun()
{                            
    static int x = 10;                   //x is static
    return x;
}
int main()
{
    fun() = 30;
    cout << fun();           //fun() called
    return 0;
}

OUTPUT:30 Can anybody tell why output is coming to be 30 and also can explain the role of static keyword

Werner Henze
  • 16,404
  • 12
  • 44
  • 69

3 Answers3

3

In computer programming, a static variable is a variable that has been allocated statically—whose lifetime or "extent" extends across the entire run of the program

void foo()
{
    int a = 10;
    static int b = 10;

    a++;
    b++;

    std::cout << "a : " << a << " , b : " << b << std::endl;
}

A reference variable is an alias, that is, another name for an already existing variable. Once a reference is initialized with a variable, either the variable name or the reference name may be used to refer to the variable.

int a = 4;
int b = a;
int &c = a;

c++;

std::cout << "b = " << b << std::endl; //4
std::cout << "a = " << a << std::endl; //5
std::cout << "c = " << c << std::endl; //5

/* Becaues c is a refence to a, it means that
a and c are just different names to the same memory location
so updating either one updates the actual value in memory
*/

a++;
std::cout << "c = " << c << std::endl;  //6
std::cout << "a = " << a << std::endl;  //6

//consider the function below:
int &bar()
{
    static int a = 5;
    std::cout << "a is " << a << std::endl;
    return a;
}

Testing the two:

int main()
{
    for (int i = 0; i < 3; i++)
        foo();
    //for every call of foo():
    //memory allocation for a is created and deleted when a goes out of scope
    //memoery allocation for b extends through out the life of the program

   //bar() returns a reference to "a" i.e
   int reference_to_a = bar(); //prints 5
   reference_to_a = 30; 
   bar();  //prints 30

   bar() = 50;  //prints 30 and later assigns 50 to the reference returned.
   bar();       //prints 50

}
Acha Bill
  • 1,255
  • 1
  • 7
  • 20
  • "whose lifetime or extent extends across the entire run of the program" - is not exactly correct. The lifetime may not begin until the first call of the function containing the static variable. And for a program with multiple static variables of class type, their lifetime ends in the opposite order of their constructors running. – M.M Aug 27 '15 at 23:57
1

static make the variable persist across function calls.

which means static int x = 10; will be executed once when func is called for the first time.

int static_test()
{
    static int x = 10;
    x++;
    return x;
} 

static_test(); // first call will return 11
static_test(); // second call will return 12 because the value of x ( was saved and was then incremented)
static_test(); // third call will return 13

Now, you need to understand what reference are. To understand what reference are you need to understand pointers. I am guessing you will easily find some website explaining those two.

Setepenre
  • 1,883
  • 3
  • 13
  • 16
  • Or OP can just just look at @jafar 's answer which covers it quite nicely. – user4581301 Aug 27 '15 at 23:09
  • @user4581301 this site does not have a restriction that nobody can post an answer if there is already an answer... – M.M Aug 27 '15 at 23:55
  • @MattMcNabb Not the point I was trying to make. Setpenre answered first by about 5 minutes, but declined to cover the effect of `&`. Jafar's answer filled that hole. – user4581301 Aug 28 '15 at 00:22
  • can anybody answer why output is coming to be 30,i already know meaning of static and references –  Aug 28 '15 at 08:08
  • @RohanBlagan, i've edited the answer to explain better why the answer is 30. – Acha Bill Aug 28 '15 at 10:55
1

case 1:

#include<iostream>
using namespace std;                     //namespace std is being used                      

int &fun()
{                            
    int x = 10;                   //x is static
    return x;
}

int main()
{
    fun() = 30;
    cout << fun();           //fun() called
    return 0;
}

Here, in the call fun(), we are declaring a local variable int x, which goes out of scope once it returns from fun(). so, in the line cout << fun() a new variable is declared and address of the new variable is returned.

case 2:

static int x = 10;    

here, since variable 'x' is static, it can be initialized only once. i.e., the first time x is initialized to 5 and then over written to 30.

now when you are making the function call subsequent times, static int x = 5 is ignored. Hence, it returns the value 30

Rjain
  • 127
  • 10