Tag Archives: linkage

Scope and Lifetime of Variables in C

Director at Feabhas Limited
Co-Founder and Director of Feabhas since 1995.
Niall has been designing and programming embedded systems for over 30 years. He has worked in different sectors, including aerospace, telecomms, government and banking.
His current interest lie in IoT Security and Agile for Embedded Systems.
Niall Cooling

In a previous posting we looked at the principles (and peculiarities) of declarations and definitions. Here I would like to address the concepts of scope and lifetime of variables (program objects to be precise).

In the general case:

The placement of the declaration affects scope
The placement of the definition affects lifetime

Lifetime

The lifetime of an object is the time in which memory is reserved while the program is executing. There are three object lifetimes:

static
automatic
dynamic

Given the following piece of code:

int global_a;       /* tentative defn; […]

Posted in C/C++ Programming | Tagged , , , | 6 Comments