码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-11-18 15:55:47      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   os   sp   for   

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++

标签:style   blog   http   io   color   ar   os   sp   for   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!