Static Variable

Introduction

  • The term static in C++ can be perplexing due to its diverse meanings in different contexts.
  • Previously, we discussed that global variables possess static duration, created at program start and destroyed at its end.
  • The static keyword provides global identifiers with internal linkage.

Static on Local Variables

Local variables have automatic duration by default, which means they are created at the point of definition, and destroyed when the block is exited.

Using static keyword on a local variable changes its duration from automatic duration to static duration. This means the variable is now created at the start of the program, and destroyed at the end of the program (just like a global variable). As a result, the static variable will retain its value even after it goes out of scope.

Applying static to a local variable changes its duration from automatic to static.

Example: Automatic Duration (Default):

#include <iostream>

void incrementAndPrint() {
    int value{ 1 }; // automatic duration by default
    ++value;
    std::cout << value << '\n';
} // value is destroyed here

int main() {
    incrementAndPrint();
    incrementAndPrint();
    incrementAndPrint();
    return 0;
}

// Output

2
2
2

Each time incrementAndPrint() is called,  variable named value is created and assigned the value of 1. incrementAndPrint() increments value to 2, and then prints the vlaue of 2. When incrementAndPrint() is finished running, the variable goes out of scope and is destroyed. Consequently, this program ooutputs:

2
2
2

Example: Duration (Using static):

#include <iostream>

void incrementAndPrint() {
    static int s_value{ 1 }; // static duration via static keyword.
    ++s_value;
    std::cout << s_value << '\n';
} // s_value is not destroyed here

int main() {
    incrementAndPrint();
    incrementAndPrint();
    incrementAndPrint();
    return 0;
}

// Output

2
3
4

In this program, because s_value has been declared as satic, it is created at the program start.

Static local variables that are zero initialized or have a constexpr initializer can be initialized at program start.

Static local variable that have no initializer or a non-constexpr initializer are zero-initialized at program start. Static local variables with a non-constexpr initializer are reinitialized the first time the variable definition is encountered. The definition is skipped on subsequent calls, so not further reinitialization happens. Because they have static duration, static local variables that are not explicitly initialized will be zero-initialized by default.

Because s_value has constexpr initializer 1, s_value will be initialized at program start.

When s_value goes out of scope at the end of the function, it is not destroyed. Each time the function incrementAndPrint() is called, the value of s_value remains at whatever we left it at previously. Consequently this program outputs:

2
3
4
  • Static local variables retain value even after going out of scope.