#include // Function declarations int main(); int f(); // Uses an ordinary local variable int g(); // Uses a static local variable // Function definitions int main() { for ( int k{ 0 }; k < 5; ++k ) { std::cout << f() << " " << g() << std::endl; } return 0; } // This function has an ordinary local variable: // - Each time the function is called, a new local // variable is initialized with the value zero. // - When the function returns, the lifetime of // the local variable ends. int f() { int n{0}; return ++n; } // This function has a static local variable: // - The static local variable is initialized only // once, with the value zero. // - When the function returns, the static local // variable continues to exist and retains its value. // - Subsequent calls to g() continue to access the // same variable without reinitializing it. int g() { static int n{0}; return ++n; }