what is static variable code example
Example 1: local static in c++
#include<iostream>
void Increment() {
int i = 0;
i++;
std::cout << i << std::endl;
};
void IncrementStaticVar() {
static int i = 0;
i++;
std::cout << i << std::endl;
}
int main() {
Increment();
Increment();
Increment();
IncrementStaticVar();
IncrementStaticVar();
IncrementStaticVar();
IncrementStaticVar();
std::cin.get();
}
Example 2: what is static variable
The static variable is used to refer to the common property of all objects
(that is not unique for each object),
e.g., The company name of employees, college name of students, etc.
Static variable gets memory only once in the class area at the time of
class loading. Using a static variable makes your program more
memory efficient (it saves memory). Static variable belongs to the class
rather than the object.