Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 1 — Scope and Lifetime of Variables

Lesson 27 of 32 in the free C Language notes on Siksha Sarovar, written by Rohit Jangra.

Scope and Lifetime of Variables in C

Scope determines where a variable can be accessed. Lifetime determines how long a variable exists in memory.

---

Types of Scope

1. Block Scope (Local)

Variables declared inside {} are accessible only within that block.

void func() {
    int x = 10;     /* block scope */
    {
        int y = 20; /* inner block scope */
        printf("%d %d\n", x, y);  /* OK */
    }
    /* printf("%d", y); — ERROR: y not accessible here */
}

2. Function Scope

Labels used with goto have function scope — visible anywhere in the function.

3. File Scope (Global)

Variables declared outside all functions are accessible throughout the file.

int globalCount = 0;  /* file scope */

void increment() { globalCount++; }
void display()   { printf("%d\n", globalCount); }

4. Program Scope (extern)

Variables declared extern can be shared across multiple files.

---

Lifetime Categories

LifetimeVariablesDuration
AutomaticLocal variablesDuration of block
Staticstatic variablesEntire program run
Dynamicmalloc/callocUntil free() is called

---

Variable Shadowing

A local variable can shadow a global variable with the same name.

int x = 100;   /* global */

void demo() {
    int x = 200;   /* local — shadows global */
    printf("%d\n", x);  /* 200, not 100 */
}

---

Static Local Variables

Static local variables retain their value between function calls.

void counter() {
    static int count = 0;  /* initialised once */
    count++;
    printf("Count: %d\n", count);
}

int main() {
    counter();  /* Count: 1 */
    counter();  /* Count: 2 */
    counter();  /* Count: 3 */
}

---

Best Practices

  • Prefer local variables over globals to reduce side effects
  • Keep variable scope as narrow as possible
  • Use meaningful names to avoid confusion when scopes overlap
  • Avoid global variables; if needed, use static to limit them to one file