Search This Blog

Thursday 27 December 2012

Static in C++

What is Static in C++?
Ans: I believe that Examples are the best way to make someone understand so:


#include <iostream>

using namespace std;
f1();
void main()
{

f1();
f1();
}
f1()
{
static int s = 0;
cout << s++ << endl;
}




The value of 's' does not start from 0 when the function is called second time, rather it starts from last value of of s when function was called last time (i.e. 1 in this case when Function is called second Time).

No comments:

Post a Comment