Course contentsShow
← Computer Science
Lesson 214 of 2,8725. Functions and ModularityFree lesson

Static Lifetime Variables

Variables that persist for the entire program execution, including global and static local variables.

Static Lifetime Variables

What you'll learn: You'll discover how certain variables can live for your program's entire run, not just inside one function call.


The Core Idea

You already know that local variables are born when a function is called and die when it returns. But sometimes you need a variable that remembers its value between function calls, or one that exists for your entire program.

Static lifetime variables persist from program start to program end. There are two main types:

Global Variables

Variables declared outside all functions. They're accessible everywhere (depending on scope rules) and live for the entire program execution.

Static Local Variables

Variables declared inside a function with a special keyword (like static in C/C++). They have local scope (only visible inside that function) but global lifetime—they remember their value between calls.

Why This Matters

Imagine a counter function that tracks how many times it's been called:

function callCount():
    static count = 0    // initialized only once, ever
    count = count + 1
    return count

First call returns 1, second returns 2, third returns 3—the variable "survives" between calls.

Analogy: Think of a local variable as a whiteboard that gets erased after each meeting. A static lifetime variable is like a notebook that stays in the conference room permanently—it remembers what was written last time.


Key Takeaway: Static lifetime variables persist throughout your program's entire execution—global variables exist everywhere for the whole runtime, while static local variables retain their values between function calls while staying locally scoped.