I have observed that if I don’t return any value from an empty function with an int
return type is 1
. But in the below case it is showing 4 3 2
as o/p(is this the value of the static variable si
getting printed here? if I print si
I will get o/p as 2 3 4, in the reverse of what I get now. is there something to do with the function's stack push and pop here in this case?). Also I observed that if I use float as return type then it prints nan nan nan
as o/p. Is this behavior compiler dependant (I have tried with both gcc and devcpp, observed the same)? What is actually going on here? Please share your thoughts on this.
#include<iostream>
using namespace std;
int f(int i){
static int si = i;
si = si + i;
///return si;
}
int main(){
cout<<f(1)<<" "<<f(1)<<" "<<f(1);
//cout<<" "<<f(1); //if I uncomment this line then the o/p is: 4 3 2 5, it looks like it's printing the value of si.
}
It looks like the behavior of cout
causing the reverse printing of static variable si
's value?