Static Variables

Lifetime and Visibility (1)

Time span where a variable is alive (its address is valid)

  • The time of a function call $to$ automatic variable

  • The entire program

Visibility of a variable

  • Inside the where it is defined ⟶ internal Variable

  • Inside the file where is is defined

  • Across the entire programx

Automatic Variables

  • Visibility: function

  • Lifetime: function

void f(void)
{
    int i;
}

Local static Variable

  • Visible only inside the function where it is defined

  • Retaines it value across function calls

  • Visible: function

  • Lifetime: program

void f(void)
{
    static int i;
}

Global static Variable

  • Visible only inside the file where it has been defined

  • Retains its value during program lifetime

  • Visible: file

  • Lifetime: program

static int i;

Global Variable

  • Visible across all files

  • Retains its value during program lifetime

  • Visible: program

  • Lifetime: Program

int i;