void func() { static int static_var=1; int non_static_var=1; static_var++; non_static_var++; cout<<"Static="<<static_var; cout<<"NonStatic="<<non_static_var; } void main() { clrscr(); int i; for (i=0;i<5;i++) { func(); } getch(); }
The above gives output as:
Static=2 Nonstatic=2 Static=3 Nonstatic=2 Static=4 Nonstatic=2 Static=5 Nonstatic=2 Static=6 Nonstatic=2
Static variable retains its value while non-static or dynamic variable is initialized to '1' every time the function is called. Hope that helps.
reference: http://stackoverflow.com/questions/5255954/what-is-the-difference-between-static-and-normal-variables-in-c