Lifetime of Variables

Lifetime

Two classes of variables …

  • Local (“automatic”). Lifetime (and visibility) is confined to the function call.

    • All variables that we had so far are automatic (function parameters are local variables)

  • Global (“extern”). Lifetime is the entire program.

    Another point of view: visibility ⟶ later

Global Variables

Global variables are evil

  • They obscure program logic

  • Side effects through unnecessary persistence

  • Global constants are ok

There are use cases though …

  • Not a use case: laziness

Global Variables: How?

  • Functions (code) can only be defined in global scope

  • Have access to local variables and other global objects

int i;

void f(void)
{
    printf("%d\n", i);
}