首页 > 代码库 > 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++
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。