CHAPTER 19
Beginner
Storage Classes
Updated: May 17, 2026
5 min read
# CHAPTER 19
Storage Classes
1. Introduction
When you declare a variable in C, you don't just specify its data type (likeint). Every variable also has a Storage Class, which tells the compiler four things:
- 1. Scope: Where can this variable be seen/accessed?
- 2. Lifetime: How long does the variable exist in memory?
- 3. Default Value: If uninitialized, what is its value (0 or garbage)?
- 4. Storage Location: Does it live in RAM or a CPU register?
2. Learning Objectives
By the end of this chapter, you will be able to:-
Explain the
autostorage class (the default).
-
Use the
statickeyword to retain values across function calls.
-
Use
externto share variables across multiple files.
-
Understand the
registerkeyword for performance optimization.
3. The auto Storage Class
This is the default storage class for all local variables. If you don't specify a class, C assumes auto.
- Scope: Local to the block/function.
- Lifetime: Destroyed when the block ends.
- Default Value: Garbage.
c
4. The static Storage Class
This is one of the most important keywords in C. When applied to a local variable, static tells the compiler to keep the variable alive even after the function ends. It is initialized ONLY ONCE.
- Scope: Local to the function (cannot be accessed outside).
- Lifetime: Exists for the entire duration of the program.
-
Default Value:
0(not garbage!).
c
*(If count was auto, it would print 1 every single time because it would be destroyed and recreated).*
5. The extern Storage Class
Used to give a reference of a global variable that is defined in *another* file. This is crucial for multi-file C projects.
- Scope: Global.
- Lifetime: Entire program.
-
Default Value:
0.
File1.c:
c
File2.c:
c
6. The register Storage Class
Used to hint to the compiler that a variable will be used heavily (like a loop counter) and should be stored directly in a CPU Register instead of RAM, making it significantly faster.
- Storage Location: CPU Register (if available).
-
Note: You cannot use the
&(address-of) operator on a register variable because CPU registers don't have RAM memory addresses.
c
*(Modern compilers are so smart they usually ignore this keyword and optimize automatically).*
7. Memory-Level Explanation
- auto: Stored on the Stack. Pushed on function call, popped on return.
- static: Stored in the Data/BSS segment (same as global variables). Not destroyed until program exit.
- extern: Points to memory in the Data/BSS segment created by another file.
- register: Stored inside the CPU itself.
8. Common Mistakes
- Assuming static variables reset: A static variable holds its state. If you expect it to start at 0 again upon calling the function later, you will have logic bugs.
-
Using
&on a register:register int x; printf("%p", &x);will cause a compiler error.
-
Defining extern variables:
extern int x = 10;is wrong.externonly *declares*, it does not *define* (allocate memory).
9. Exercises
-
1.
Write a function with an
autovariable and astaticvariable. Increment both and call the function 5 times to see the difference.
-
2.
What happens if you try to access a
staticvariable defined infunc1()from insidefunc2()? Try it and observe the error.
10. MCQ Quiz with Answers
Question 1
What is the default storage class for local variables?
Question 2
Which storage class retains its value between function calls?
Question 3
What is the default initial value of a static variable?
Question 4
Where are static variables stored in memory?
Question 5
Which keyword is used to access a global variable defined in another file?
Question 6
Where is a register variable ideally stored?
& operator on a register variable?
a) Yes b) No
Answer: b) No
Question 8
The scope of an auto variable is:
Question 9
Which storage class is best for a loop counter running 10 million times?
Question 10
What does extern do?
11. Interview Questions
- Q: What is the difference between a global variable and a static local variable?
-
Q: If I have a global variable
int x = 5;, but I putstaticin front of it (static int x = 5;), what happens? (Answer: It restricts the global variable's scope to ONLY that specific C file).
-
Q: Explain why modern compilers often ignore the
registerkeyword.
12. Summary
Storage classes dictate Scope, Lifetime, Default Value, and Location.auto is for normal local variables. static keeps variables alive across function calls. extern shares variables across files. register requests high-speed CPU storage.
13. Next Chapter Recommendation
In Chapter 20: Preprocessor Directives, we will look at how C prepares your code before it even starts compiling using#include, #define, and macros.