Storage Classes in C: auto, register, static, extern | Unit 3 | BKNMU BCA
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
auto, register, static, and extern. This comprehensive guide covers all four for BKNMU BCA Semester 1 (Unit 3).
Variables are categorized into four storage class types based on their memory and lifetime characteristics:
1. Automatic (Local)
Default class for all local variables. Stored on the stack frame, holds garbage values initially, and dies when the block exits.
2. CPU Register
Stored directly in high-speed CPU registers for fast execution access (e.g., loop counters). Cannot use the address-of operator (&).
3. Persistent Static
Retains its updated value between multiple function calls throughout the entire program run. Defaults automatically to 0.
4. External (Global)
Global variable accessible across multiple files and functions. Declared outside functions and shared across project modules.
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 |
- 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.
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 1: Program Initialization (Data Segment)
Before main() starts, static and extern variables are allocated in the data segment and initialized to zero automatically.
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.
Step 3: Block Exit & Teardown
When the block ends, auto and register memory is destroyed. static variables remain alive, retaining changes for future calls.
autovariables lose their modified values once the function completes execution.staticvariables are initialized only once during the entire program run and retain their last modified value across multiple function calls.
&) to a register variable (e.g. &i is illegal) because CPU registers do not have physical RAM memory addresses.
- 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.