Unit 10: Storage Classes
Table of Contents
1. Introduction to Storage Classes
In C, every variable has two attributes: a type (like int or float) and a storage class. A storage class defines the scope (visibility) and lifetime of a variable within a program.
Definition: Storage classes tell the compiler where to store the variable, what the initial value is, and how long the variable exists.
2. Scope, Visibility, and Lifetime of Variables
Before diving into specific specifiers, it is critical to understand three key concepts:
- Scope: The region of the program where a variable is accessible (e.g., local to a function or global to the entire file).
- Visibility: Whether a variable is "seen" at a particular point in the code. A local variable can "hide" a global variable of the same name.
- Lifetime (Extent): The duration for which a variable remains in the computer's memory during program execution.
3. Storage Class Specifiers
C provides four primary storage class specifiers:
| Specifier | Storage | Initial Value | Scope | Lifetime |
|---|---|---|---|---|
| auto | Stack (Memory) | Garbage | Local | Function Block |
| register | CPU Register | Garbage | Local | Function Block |
| static | Data Segment | Zero | Local/Global | Entire Program |
| extern | Data Segment | Zero | Global | Entire Program |
4. Automatic Storage Class (auto)
The auto storage class is the default for all local variables declared inside a function or block.
void function() { int x; // same as "auto int x;" } - Storage: Main memory (Stack).
- Lifetime: Created when the block is entered and destroyed when the block is exited.
5. Register Storage Class (register)
The register specifier suggests to the compiler that the variable be stored in a CPU register instead of RAM for faster access.
register int counter;
- Use Case: Variables used frequently, such as loop counters.
- Warning: You cannot use the address operator (&) on a register variable because it does not have a memory address.
6. Static Storage Class (static)
The static storage class instructs the compiler to keep a local variable in existence during the entire lifetime of the program.
Static Local Variables
Unlike auto variables, static local variables maintain their values between function calls.
void increment() { static int count = 0; count++; printf("%d", count); } // Calling increment() thrice will print 1, 2, 3. Static Functions
When a function is declared as static, its scope is restricted to the file in which it is defined. This is used for encapsulation and data hiding.
7. External Storage Class (extern)
The extern storage class is used to give a reference to a global variable that is visible to all the program files.
// File1.c int count = 5; // File2.c extern int count; // Declares that count is defined elsewhere void main() { printf("%d", count); } 8. Const Qualifier
The const qualifier is used to declare variables whose value cannot be changed after initialization.
const float PI = 3.14; // PI = 3.15; // This will cause a compilation error
- Purpose: To protect data from accidental modification and improve code readability.
- Scope: Follows the same scope rules as the storage class it is used with.
9. Exam Focus Enhancements
- Initialization: Remember that static and extern variables are initialized to 0 by default, while auto and register contain garbage values.
- Static Local Use: Use static local variables if you need to "remember" a value across different calls to the same function (like a call counter).
- Global vs Extern: A global variable is defined at the top of a file. Use
externonly when you want to access that variable in a different file.
- Register Limit: Not all variables can be stored in registers. If the CPU registers are full, the compiler treats
registerlikeauto. - Const Assignment: Trying to declare a
constvariable without initializing it immediately. Since you can't change it later, an uninitializedconstis useless. - Static Scope: Thinking
staticmakes a variable accessible everywhere. Astatic localvariable still has local scope; it just has a permanent lifetime.
Q: Can I use 'extern' inside a function?
A: Yes, it tells the function that the variable it is using is a global one defined either later in the same file or in another file.
Q: What is the difference between 'static global' and 'global'?
A: A regular global variable can be accessed by other files using extern. A static global variable is hidden from other files.
Q: Why use 'const' instead of '#define'?
A: const variables have a data type and follow scope rules, making them safer and easier to debug than #define macros.