ccplusplus.com
Learn C, C++ Concepts
Sunday, March 11, 2012
static variable code c++
/******************************************************************* * File : static-variable.cpp * Author : Saurabh Gupta * Description : static variable code c++ * Date : * Source : http://www.ccplusplus.com/p/c.html * Note : *******************************************************************/ #include
using namespace std; void func(void); static int nCount = 10; /* Global variable - static is the default */ int main() { while (nCount --) { func(); } } void func( void ) { static int nI = 5; nI++; cout << "nI is " << nI << " and nCount is " << nCount << endl; } /* * OUTPUT * [sgupta@rhel6x64 variables]$ c++ C2_3_3.cpp -o C2_3_3 [sgupta@rhel6x64 variables]$ ./C2_3_3 nI is 6 and nCount is 9 nI is 7 and nCount is 8 nI is 8 and nCount is 7 nI is 9 and nCount is 6 nI is 10 and nCount is 5 nI is 11 and nCount is 4 nI is 12 and nCount is 3 nI is 13 and nCount is 2 nI is 14 and nCount is 1 nI is 15 and nCount is 0 [sgupta@rhel6x64 variables]$ */
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment