
Dibyendu D. answered 02/26/20
5+ years of experience in programming and teaching C++
What exactly is its lifetime?
The space for the static variable is allocated only one time and this is used for the entirety of the program.
Once this variable is declared, it exists till the program executes. So, the lifetime of a static variable is the lifetime of the program.
When do its constructor and destructor get called?
Let's demonstrate it with the help of a static object in C++
Output:
If we remove the static keyword from the function foo(), we get the output as below: