+ 7

Static variable

int& func() { static int x =3; return x; } int main () { func() = 1; cout << func(); return 0; } why this code prints 1 instead of 3. I know that first line in main gets variable x and assign value as 1. second line print value of x.... Question is that why static variable x doesn't retain value through out problem.... if static is not there inside function, output of code can be easily understandable as 1.

25th Mar 2018, 10:45 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
10 odpowiedzi
+ 5
Finally I got the answer why it prints 1 and not 3 for original source code..... Here is the answer: func is called twice from main .. once from first line (func()=1) and second from cout statement... as x has been declared as static, line static int x will be executed only once.... so, it will be executed in first function call and x becomes 3... as soon as this func execution is over and control is passed to main function, func()=1 becomes x =1. this changes x from 3 to 1... Now when func is called second time, only return x is executed and cout << func() becomes cout << x which is nothing but 1.... thanks guys for your replies.... hope this answer helps...and special thanks to the one who prepared this quiz in sololearn....
3rd Jun 2018, 8:45 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 4
correct. I missed int after static.... and if x is second time in memory, then y value of x is not getting updated from 3 to 1 during second time function call I.e. function called on line of cout.
25th Mar 2018, 11:05 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
yes , there was mistake in code . I missed int data type in function...I updated code now in original post... what if below code is there: int& func() { int x =3; return x; } int main () { func() = 1; cout << func(); return 0; } now, data type is there but static is not there... what should be output and wHy?
25th Mar 2018, 5:48 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
Ketan Lalcheta did you get resolution on this?
30th May 2018, 9:57 AM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
+ 1
Static in this case just means that x will not be deleted once the function finished, but will stay in the memory. So if you execute the function for the second time x is still the same. The problem with your code is at least that x has no type so there's just an error and I can't reproduce your output. But it should (if x was an int) return 3.
25th Mar 2018, 11:01 AM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 1
Static is not a datatype.. U must use static Int... To assign the value... Because of no Int there is no place to store.. At first function call it return x. That's is 1.. So it prints 1
25th Mar 2018, 5:13 PM
T Indrajith
T Indrajith - avatar
+ 1
3.consider second func call.. It return x. Which is 3..
25th Mar 2018, 5:56 PM
T Indrajith
T Indrajith - avatar
+ 1
not yet...
31st May 2018, 3:09 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
No...I also expected answer as 3, but it fails to build and tells me that one cannot return local variable as reference from function because scope of local variable ends with end of function.... this made me believe strongly that static variable in original post code should retain value as 3 and it should print 3 as output.... but fact remains same that output of original code is 1 and second code fails to build
25th Mar 2018, 6:02 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
You didn't declare the variable in the int main? Or maybe because the func() is defined?
30th Mar 2018, 12:06 AM
Squidy P
Squidy P - avatar