🧭 Navigation
⬅️ Previous | 🏠 Home | ➡️ Next
Storage classes in C determine the scope, lifetime, and memory location of variables. They control how variables are stored in memory and how long they persist during program execution.
- Default storage class for local variables
- Variables are stored in stack memory
- Scope: Local to the block/function where declared
- Lifetime: Exists only within the block/function
- Initial value: Garbage value (uninitialized)
#include <stdio.h>
int main() {
auto int x = 10; // 'auto' is optional, same as: int x = 10;
{
auto int y = 20; // Local to this block
printf("y = %d\n", y);
}
// y is not accessible here
printf("x = %d\n", x);
return 0;
}- Suggests to compiler to store variable in CPU registers for faster access
- Scope: Local to the block/function where declared
- Lifetime: Exists only within the block/function
- Initial value: Garbage value
- Note: Compiler may ignore this suggestion
#include <stdio.h>
int main() {
register int counter; // Suggests storing in CPU register
for(counter = 0; counter < 1000; counter++) {
// Fast access for loop counter
}
return 0;
}- Variables retain their value between function calls
- Scope: Local to the block/function where declared
- Lifetime: Entire program execution
- Memory: Data segment
- Initial value: Zero (0) by default
#include <stdio.h>
void function() {
static int count = 0; // Retains value between calls
count++;
printf("Function called %d times\n", count);
}
int main() {
function(); // Output: Function called 1 times
function(); // Output: Function called 2 times
function(); // Output: Function called 3 times
return 0;
}Static Global Variables:
#include <stdio.h>
static int globalVar = 100; // Only accessible within this file
void func() {
printf("Global var: %d\n", globalVar);
}- Used to declare variables that are defined elsewhere
- Scope: Global (entire program)
- Lifetime: Entire program execution
- Memory: Data segment
- Purpose: Link variables across multiple source files
File 1 (main.c):
#include <stdio.h>
extern int globalVar; // Declaration only
int main() {
printf("Global var: %d\n", globalVar);
return 0;
}File 2 (globals.c):
int globalVar = 50; // Definition- Variables cannot be modified after initialization
- Scope: Depends on where declared
- Lifetime: Depends on storage class used
- Memory: Usually in data segment
#include <stdio.h>
int main() {
const int MAX_SIZE = 100;
const float PI = 3.14159;
// MAX_SIZE = 200; // Error: cannot modify const variable
printf("Max size: %d\n", MAX_SIZE);
printf("PI: %f\n", PI);
return 0;
}Memory Layout:
┌─────────────────┐
│ Stack │ ← auto, register variables
│ │
├─────────────────┤
│ Heap │ ← dynamically allocated memory
│ │
├─────────────────┤
│ Data Segment │ ← static, extern, const variables
│ (Initialized) │
├─────────────────┤
│ BSS Segment │ ← uninitialized static variables
│ │
├─────────────────┤
│ Code Segment │ ← program instructions
└─────────────────┘
| Storage Class | Scope | Lifetime | Memory | Initial Value | Keyword |
|---|---|---|---|---|---|
| auto | Local | Block | Stack | Garbage | auto (optional) |
| register | Local | Block | CPU Register | Garbage | register |
| static | Local | Program | Data Segment | 0 | static |
| extern | Global | Program | Data Segment | 0 | extern |
| const | Depends | Depends | Data Segment | Must initialize | const |
- Use
staticfor variables that need to persist between function calls - Use
externfor sharing variables across multiple source files - Use
constfor values that should never change - Avoid
registerin modern C (compilers are smart enough) autois rarely explicitly used (it's the default)
int getNextId() {
static int id = 0;
return ++id;
}// header.h
extern int sharedVariable;
// source.c
int sharedVariable = 42;const int BUFFER_SIZE = 1024;
const char* CONFIG_FILE = "config.txt";- Modern C compilers are smart about optimization
registerkeyword is often ignoredstaticvariables are optimized for memory accessconstvariables may be placed in read-only memory- Storage class affects variable initialization and cleanup
🧭 Navigation
⬅️ Previous | 🏠 Home | ➡️ Next