您的位置:首页 > 编程语言 > C语言/C++

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

2014-11-18 15:42 736 查看
voidfunc()
{
staticintstatic_var=1;
intnon_static_var=1;

static_var++;
non_static_var++;

cout<<"Static="<<static_var;
cout<<"NonStatic="<<non_static_var;
}

voidmain()
{
clrscr();
inti;
for(i=0;i<5;i++)
{
func();
}
getch();
}



Theabovegivesoutputas:

Static=2
Nonstatic=2

Static=3
Nonstatic=2

Static=4
Nonstatic=2

Static=5
Nonstatic=2

Static=6
Nonstatic=2



Staticvariableretainsitsvaluewhilenon-staticordynamicvariableisinitializedto'1'everytimethefunctioniscalled.Hopethathelps.

reference:http://stackoverflow.com/questions/5255954/what-is-the-difference-between-static-and-normal-variables-in-c
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐