From a30d605024322ada2206d10898b8dfd6220107c1 Mon Sep 17 00:00:00 2001 From: Oskar Date: Thu, 5 Sep 2024 10:45:59 +0200 Subject: more --- 6p6.cpp | 14 ++++++++++++++ 6p7.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 6p6.cpp create mode 100644 6p7.cpp diff --git a/6p6.cpp b/6p6.cpp new file mode 100644 index 0000000..d8963f7 --- /dev/null +++ b/6p6.cpp @@ -0,0 +1,14 @@ + +/* + * + * 6.6 + * + * + */ + +int main () { + + // When the function ends the local variable is destroyed + // When the function ends the static variable retains its value and is still there when the function is called again. + return 0; +} diff --git a/6p7.cpp b/6p7.cpp new file mode 100644 index 0000000..2c16109 --- /dev/null +++ b/6p7.cpp @@ -0,0 +1,30 @@ +#include + +/* + * + * 6.7 + * + * + */ + +int ThisFunctionHasAStaticVariable() { + + static int i = -1; + ++i; + return i; +} + +int main () { + + std::cout << ThisFunctionHasAStaticVariable() << std::endl; + std::cout << ThisFunctionHasAStaticVariable() << std::endl; + std::cout << ThisFunctionHasAStaticVariable() << std::endl; + std::cout << ThisFunctionHasAStaticVariable() << std::endl; + std::cout << ThisFunctionHasAStaticVariable() << std::endl; + std::cout << ThisFunctionHasAStaticVariable() << std::endl; + std::cout << ThisFunctionHasAStaticVariable() << std::endl; + std::cout << ThisFunctionHasAStaticVariable() << std::endl; + std::cout << ThisFunctionHasAStaticVariable() << std::endl; + std::cout << ThisFunctionHasAStaticVariable() << std::endl; + return 0; +} -- cgit v1.2.3