static locals get initialized only once. normal locals get initialized every time their function is called. Also, static locals are not on stack, that is, &num will never change between calls to shownum(), while &num2 could (although in this example wont).
it should be clear when you decide to use static. or if you got this code from somewhere else, you should understand the whole code first... well, i'm not an expert.. it's just my personal (humbly) opinion...
I am guessing this is an example to show you the difference between static and non-static variables, right?
Well a static variable is a special case variable and it's only created and initialized once in a function. That is the 1st time this function is called. After that in every call this variable just exist and you can use it's previous value.
static is your num variable, so in every loop it changes its value.
The other variable is NOT static. So it gets allocated and initialized in EVERY call of the function. It is changing though. It's just that you display it before change it. Line 22 has no visual effect on you so you think it's n ot changing.
Try a variation of your program like:
1 2 3 4 5 6 7 8 9 10 11
void shownum()
{
staticint num=5;
int num2=10;
num++;
num2++;
cout<<"num is "<<num<<endl;
cout<<"num2 is "<<num2<<endl;
}
You made incorrect conclusion. Your variable num2 is adding 1 for each loop. Just insert ouputing num2 before leaving function shownum and you will see that num2 was changed
void shownum()
{
static int num=5;
int num2=10;
cout<<"num is "<<num<<endl;
cout<<"num2 is "<<num2<<endl;
num++;
num2++;
cout<<"num is "<<num<<endl;
cout<<"num2 is "<<num2<<endl;
}
That is, initially num2 had value 10 and after increment it had value 11. Every time then you call your function shownum you assign to num2 value equal to 10 and increment it. The difference between num2 and num is that in this simple case (then a static variable is initialized by a constant expression) num is initialized only once before any call to function shownum because its storage duration is static.
#include <iostream>
//#include <conio> // get rid of this, it is not a standard C++ header
void shownum();//Function prototype
int main()
{
for( int count = 0 ; count < 5 ; ++count ) shownum();
// getch();
char wait ; std::cin >> wait ;
}
void shownum()
{
// the comma in the initialization is the sequencing operator
// http://en.wikipedia.org/wiki/Comma_operatorstaticint num = ( (std::cout << "initialize static int num with 5\n") , 5 ) ;
int num2 = ( (std::cout << "initialize (automatic) int num2 with 10\n") , 10 ) ;
++num ;
++num2 ;
std::cout << "num is " << num << '\n';
std::cout << "num2 is " << num2 << '\n';
}
Thanks @eypros & @vlad from moscow
Better understanding now...Just to confirm...Is that means, static variable only can initialize once in a function? I mean even I go many many time into the same function, that static variable will only go through ONCE(Only the very 1st TIME) even I go into that function another time infinitely?
And if I put it like this, meaning that we can't shows what that static variable are actually right?
Thanks JLBorges
Actuall I use getch(); and #include <conio> , so that my window will not close after shows the output...and you mean dont' use getch();, so is that it can be replace with both of this line below?
1 2
char wait ;
cin >> wait ;
What does that line mean actually? Is that wait is a variable?
// the comma in the initialization is the sequencing operator
What do u mean by