Storage Classes in C
Storage Classes in C
The `auto` storage class is the Variables with `auto` storage class Variables with `auto` storage class
default for local variables. are created when the block of code can only be accessed within the
they are declared in is entered and block where they are defined.
destroyed when the block is exited.
Register Storage Class:
Optimization Hint
Speed Boost No Guarantee
The `register` storage The compiler may ignore
class suggests to the this suggestion if it cannot
compiler that the variable allocate a register.
should be stored in a CPU
register for faster access.
Address Restriction
You cannot take the address of a register variable using the
`&` operator.
Static Storage Class: Persistent Variables
Persistence Limited Scope Program Lifetime
Variables declared with the Static variables are still local in They exist for the entire duration
`static` storage class retain their scope and cannot be accessed of the program, even though
values between function calls. outside the function or file where they are only accessible within
they are defined. the block or file.
Extern Storage Class: Global
Declarations
Global Scope
The `extern` storage class declares a variable or function that is defined in another file.
Global Visibility
The variable or function is visible to the whole program, but it must be defined elsewhere
Program Lifetime
Variables or functions declared with `extern` storage class exist for the entire
duration of the program.
Using Storage Classes Effectively
Code Organization
Memory Management
Understanding storage classes improves the organization and
Storage classes help you control the memory usage of your programs. maintainability of your C code.
1 2 3
Lifetime
They determine how long variables and functions
exist in memory.
Visibility
Storage classes influence which parts of the program
can see and use variables and functions.