C++ examples for Function:static variable
Local static variables is initialized the first time the statement is executed rather than when the program is compiled.
#include <iostream> using namespace std; void teststat(); // function prototype int main()//from w w w . j av a 2s . com { int count; // count is a local auto variable for (count = 1; count <= 3; count++) teststat(); return 0; } void teststat() { static int num = 0; // num is a local static variable cout << "The value of the static variable num is now " << num << endl; num++; return; }