C Programming - Beginner to Brilliance / Functions in C

Scope and Lifetime of Variables

When you create a variable in C, you must understand two important characteristics of it:

🔍 Scope — “Where” a variable can be accessed

Think of scope as the “visibility” of a variable — it defines in which parts of your code the variable is known and can be used.


⏳ Lifetime — “How long” the variable exists in memory

Think of lifetime as the “duration” for which the variable stays in memory (RAM) during the program's execution.


🔍 Scope of Variables in C

Scope is divided into 4 major types:

1️⃣ Local Scope (Block Scope)

  • A variable declared inside a function or a block ({}) is called a local variable.

  • It can only be accessed inside that function/block.

  • It is created when the block is entered and destroyed when the block ends.

📌 Example:

void myFunction() {
    int x = 10;  // x has local scope
    printf("%d", x);
}
// printf("%d", x); ❌ This will give an error: x is not accessible here

2️⃣ Global Scope

  • A variable declared outside all functions, usually at the top of the file.

  • It can be accessed by all functions in that file (or across files with extern).

  • It is created when the program starts and destroyed when the program ends.

📌 Example:

int x = 5;  // Global variable

void show() {
    printf("%d", x);  // Accessible
}

int main() {
    printf("%d", x);  // Also accessible
}

3️⃣ Function Parameters Scope

  • Parameters in functions act like local variables.

  • They are created when function is called and destroyed when function ends.

📌 Example:

void show(int num) {
    printf("%d", num);  // num has scope only within show()
}

4️⃣ File Scope (with static)

  • If you declare a global variable with static, it will only be visible within the current file — not accessible from other files.

📌 Example:

static int counter = 0;  // File scope, accessible only within this file

⏳ Lifetime of Variables in C

Let’s now understand how long variables stay in memory, based on their storage class.

🧱 Automatic (Local) Variables

  • Declared inside a function without static.

  • Lifetime: From function entry to function exit.

  • Stored in stack memory.

  • Declared with auto (default, so often skipped).

void show() {
    int x = 10;  // automatic variable
}

🗃️ Static Variables

  • Declared with static keyword (can be local or global).

  • Lifetime: Entire program run.

  • Retains value between function calls.

  • Stored in data segment (not stack).

📌 Example:

void counterFunc() {
    static int count = 0;  // Initialized only once
    count++;
    printf("%d\\n", count);
}

int main() {
    counterFunc(); // 1
    counterFunc(); // 2
    counterFunc(); // 3
}

🌍 Global Variables

  • Declared outside any function.

  • Lifetime: Entire program.

  • Accessible from all functions in the same file.

int x = 100;

void test() {
    printf("%d", x);  // Still alive here
}

🌐 Extern Variables

  • Declared in another file, accessed using extern.

  • Lifetime is also for the entire program.

// file1.c
int x = 100;

// file2.c
extern int x;

🧪 Summary Table

Type

Scope

Lifetime

Keyword

Stored In

Local (auto)

Inside block

Till block ends

auto (default)

Stack

Global

Entire program

Entire program

None

Data segment

Static (local)

Inside block

Entire program

static

Data segment

Static (global)

Current file only

Entire program

static

Data segment

Extern

Cross-file access

Entire program

extern

Data segment


🔄 Analogy: Library Books 📚

Variable Type

Analogy

Local Variable

Book you borrow for a day (return after reading)

Static Variable

A personal diary you keep adding entries to

Global Variable

A notice board accessible to everyone in the library

Extern Variable

A shared online document from another building


🧠 Golden Tips

  • If two variables have the same name, the local variable will shadow the global one inside the function.

  • Using static inside a function preserves values between calls — very useful for counting, caching, etc.

  • Always initialize variables — especially static and global ones.