Static Functions

What's the Difference Between Static and Non-Static Variables?

Static variables keep their value for the whole program, while ordinary local variables are recreated on each call. See examples of both in C++.

On this page
  1. Lifetime and copies
  2. Local variables
  3. Class members
  4. Global variables
  5. A quick comparison

Lifetime and copies

A static variable exists for the whole run of the program. An ordinary local variable exists only while its block runs: it is created when the block starts and destroyed when the block ends.

A non-static class member lives as long as its object, and each object has its own copy. A static class member has one copy for the whole class.

Local variables

#include <iostream>

void demo() {
    int automatic_count = 0;      // set to 0 on every call
    static int static_count = 0;  // set to 0 only once, on the first call

    ++automatic_count;
    ++static_count;
    std::cout << automatic_count << ' ' << static_count << '\n';
}

int main() {
    demo();  // prints 1 1
    demo();  // prints 1 2
    demo();  // prints 1 3
}

The first number stays at 1 because automatic_count starts over at 0 on each call. The second number grows because static_count keeps its value between calls.

Ordinary local variables are usually kept on the stack, a region of memory that is reused as functions are entered and left. For how that compares to heap memory, see heap vs stack memory in C++.

Class members

A non-static data member gets one copy in each object. A static data member gets one copy for the class:

#include <iostream>

class Sensor {
public:
    Sensor() { ++installed; }  // every new Sensor increments the shared count

    int reading = 0;           // non-static: each Sensor has its own copy
    static int installed;      // static: one copy shared by all Sensors
};

int Sensor::installed = 0;     // the out-of-class definition is required

int main() {
    Sensor a;
    Sensor b;
    a.reading = 5;
    b.reading = 9;

    std::cout << a.reading << ' ' << b.reading << '\n';  // prints 5 9
    std::cout << Sensor::installed << '\n';              // prints 2
}

From C++17, you can write inline static int installed = 0; inside the class instead of defining it outside.

Global variables

A variable at file scope already has static storage duration: it lives for the whole run, even without the keyword. Adding static to it changes its linkage instead, so the name is visible only in its own file:

// settings.cpp
static int retry_limit = 3;  // only settings.cpp can use this name

A quick comparison

Kind How long it lives How many copies
Local variable While its block runs One per call
static local variable Whole program run One
Non-static class member While its object lives One per object
static class member Whole program run One per class
Global variable Whole program run One, which other files can reach with extern
static global variable Whole program run One, visible in its file only

Choose static when a value should persist, and leave it off when each use should start fresh.

The complete guide to static functions

Static Functions in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

Enums in C++ With Examples

Learn enums in C++ with clear examples: syntax, automatic values, enum class, switch statements, underlying types, and common mistakes to avoid.

Enums 5 min read