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:
相关文章推荐
- What is the difference between @staticmethod and @classmethod in Python?
- What exactly is the difference between “pass by reference” in C and in C++?
- What is the difference between static and global variables ?
- What is the difference between const and static readonly?(const 和 static readonly 的区别?)
- whats is the difference between train, validation and test set, in neural networks?
- What is the difference between SVD and matrix factorization in context of recommendation engine?
- macro与inline的区别 What is the difference between macro and inline?
- What is the difference between _tmain() and main() in C++?
- What Is the Difference Between a Block, a Proc, and a Lambda in Ruby
- what is the difference between definition and declaration in c
- What is the difference between SET and SELECT when assigning values to variables, in T-SQL?
- What is the Difference Between Type and Class in
- OpenStack: What is the difference between Block storage and Object storage?Also, In what way they ar
- Whats is the difference between train, validation and test set, in neural networks?
- What is the difference between a soft reference and a weak reference in Java?
- What is the difference between provider network and self-service network in OpenStack?
- What are the differences between struct and class in C++?
- What is the difference between Views and Materialized Views in Oracle?
- What are the differences between a pointer variable and a reference variable in C++?
- What are the differences between a pointer variable and a reference variable in C++?