首页 > 编程语言 > 详细

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

时间:2014-11-18 15:53:14      阅读:265      评论:0      收藏:0      [点我收藏+]
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

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

原文:http://www.cnblogs.com/bitter/p/4105955.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!