num2++??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <conio>

void shownum();//Function prototype

int main()
{
	for (int count = 0; count <5; count++)
		shownum();
   getch();
   return 0;
}

void shownum()
{
	static int num=5;
	int num2=10;

   cout<<"num is "<<num<<endl;
   cout<<"num2 is "<<num2<<endl;
   num++;
   num2++;
}


Hi, can I know why my num2 is not adding 1 for each loop?

TQ...
Last edited on
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...
@hamsterman

u mean static will not change right? But this is the output i get..

num is 5
num2 is 10
num is 6
num2 is 10
num is 7
num2 is 10
num is 8
num2 is 10
num is 9
num2 is 10


the num2 is not changing even it's not static?

TQ...
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()
{
	static int num=5;
	int num2=10;

   num++;
   num2++;
   cout<<"num is "<<num<<endl;
   cout<<"num2 is "<<num2<<endl;

}
to see num2 change also

Last edited on
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.
Last edited on
Run this and try to understand the output that is produced:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#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_operator
	
    static int 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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <conio>

void shownum();//Function prototype

int main()
{
       static int num=5;
	         int num2=10;
	for (int count = 0; count <5; count++)
	{	

               cout<<"num is "<<num<<endl;
                 cout<<"num2 is "<<num2<<endl;
                 num++;
                 num2++;
          }
   getch();
   return 0;
}
Last edited on
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
sequencing operator
?

Thanks...
Last edited on
Is that wait is a variable?

yes, wait is a variable (, you declare it with char type)

it's just a tricky way to make your program waits (because it waiting for an input (in this case, it waits for an input for variable wait

for better understanding, you can read or download from this page:

http://cplusplus.com/doc/tutorial
Last edited on
Thanks chipp, then how to input into wait? And can I declare it as int instead of char? TQ...
Topic archived. No new replies allowed.