| Storage Class |
Scope |
Storage Location |
Default Value |
Keyword |
Usage |
| auto |
Local to block or function |
Stack (RAM) |
Garbage (random) |
auto (optional) |
Local variables in functions |
| static |
Local to block, value persists |
Data Segment (RAM) |
0 (if uninitialized) |
static |
Maintains state across calls |
| register |
Local to block or function |
CPU Register (if available) |
Garbage (random) |
register |
For fast access variables |
| extern |
Global across multiple files |
Data Segment (RAM) |
0 (if uninitialized) |
extern |
Global variables declared elsewhere |
#include <stdio.h>
int globalVar = 5; // extern by default
void staticDemo() {
static int count = 0;
count++;
printf("Static variable count: %d\n", count);
}
void autoDemo() {
auto int localVar = 10;
printf("Auto local variable: %d\n", localVar);
}
void registerDemo() {
register int fastVar = 15;
printf("Register variable: %d\n", fastVar);
}
int main() {
printf("Global variable: %d\n", globalVar);
staticDemo();
staticDemo();
autoDemo();
registerDemo();
return 0;
}