首页 > 代码库 > what is the difference between static and normal variables in c++

what is the difference between static and normal variables in c++

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=2Nonstatic=2Static=3Nonstatic=2Static=4Nonstatic=2Static=5Nonstatic=2Static=6Nonstatic=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

what is the difference between static and normal variables in c++