BCA SCHOOL
Programming Tutorials • Notes • Practical • Books
LATEST TUTORIALS

Storage Classes in C: auto, register, static, extern | Unit 3 | BKNMU BCA

by BCA School



BKNMU Junagadh  |  BCA Sem 1  |  Unit 3

Storage Classes in C: auto, register, static, and extern

Complete Guide to Variable Storage Location, Default Initial Value, Scope Visibility, and Longevity

BKNMU Junagadh BCA Sem 1 Unit 3 Storage Classes Scope & Lifetime Memory Management
In simple words: In C, every variable possesses both a data type (which defines the kind of value it holds) and a storage class (which dictates where the variable is stored in memory, its initial default value, its visible scope, and how long it remains in RAM). C supports 4 storage classes: auto, register, static, and extern. This comprehensive guide covers all four for BKNMU BCA Semester 1 (Unit 3).
📖 The 4 Storage Classes in C

Variables are categorized into four storage class types based on their memory and lifetime characteristics:

auto

1. Automatic (Local)

Default class for all local variables. Stored on the stack frame, holds garbage values initially, and dies when the block exits.

register

2. CPU Register

Stored directly in high-speed CPU registers for fast execution access (e.g., loop counters). Cannot use the address-of operator (&).

static

3. Persistent Static

Retains its updated value between multiple function calls throughout the entire program run. Defaults automatically to 0.

extern

4. External (Global)

Global variable accessible across multiple files and functions. Declared outside functions and shared across project modules.

1️⃣ Master Table: Storage Class Properties Matrix

A complete side-by-side comparison of storage location, default values, scope visibility, and variable lifetimes:

Storage Class Storage Location Default Value Scope (Visibility) Lifetime (Longevity)
auto RAM (Stack) Garbage value Local to the block where declared Within the block execution
register CPU Register (or RAM) Garbage value Local to the block where declared Within the block execution
static RAM (Data Segment) Zero (0) Local to the block where declared Entire program duration
extern RAM (Data Segment) Zero (0) Global (Accessible in all files) Entire program duration
ℹ️ The 4 Defining Features of Any Storage Class:
  • Storage: The physical memory location (CPU Register or RAM Stack / Data Segment).
  • Default Initial Value: The value assigned if no explicit initialization is provided (Garbage vs Zero).
  • Scope: The region of the program where the variable can be accessed directly (Block / Local vs Global).
  • Lifetime (Longevity): The duration for which the variable exists in memory before destruction.
💻 Practical Code Examples

1. Comparison of auto vs static across Multiple Function Calls:

#include <stdio.h>

void counterDemo(void) {
    auto int a = 1;    // Re-created and re-initialized every call
    static int s = 1;  // Initialized only once, preserves value

    printf("auto a = %d | static s = %d\n", a, s);

    a++;
    s++;
}

int main() {
    printf("--- Call 1 ---\n");
    counterDemo();

    printf("--- Call 2 ---\n");
    counterDemo();

    printf("--- Call 3 ---\n");
    counterDemo();

    return 0;
}

2. High-Speed register and Global extern Variable Demonstration:

#include <stdio.h>

// Global variable declaration (allocated in Data Segment)
int global_count = 100;

void displayExternal(void) {
    extern int global_count; // Refers to external global definition
    printf("Global extern value: %d\n", global_count);
}

int main() {
    // Fast register variable for loop performance
    register int i;
    int sum = 0;

    for (i = 1; i <= 5; i++) {
        sum += i;
    }

    printf("Loop Sum using register variable: %d\n", sum);
    displayExternal();

    return 0;
}
🔄 Step-by-Step Flow: Memory Lifecycle Comparison
1

Step 1: Program Initialization (Data Segment)

Before main() starts, static and extern variables are allocated in the data segment and initialized to zero automatically.

2

Step 2: Function Execution (Stack & Register)

Upon calling a function, auto variables are pushed onto the stack. If free CPU registers exist, register requests are placed into CPU cache registers.

3

Step 3: Block Exit & Teardown

When the block ends, auto and register memory is destroyed. static variables remain alive, retaining changes for future calls.

💡 Critical Exam Difference: auto vs static:
  • auto variables lose their modified values once the function completes execution.
  • static variables are initialized only once during the entire program run and retain their last modified value across multiple function calls.
⚠️ Important Limitation: Pointer Address on register Variables: You cannot apply the address-of operator (&) to a register variable (e.g. &i is illegal) because CPU registers do not have physical RAM memory addresses.
📝 Quick Revision — Key Exam Points
  • auto: Default for local variables; stored on Stack; initial value is Garbage.
  • register: Stored in CPU registers for speed; cannot use address operator (&).
  • static: Retains value between function invocations; default initial value is 0.
  • extern: Global scope; shared across multiple functions and source files.
  • Scope vs Lifetime: Scope is where a variable is visible; Lifetime is how long it exists in RAM.