Skip to main content
C Programming Basics
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 (like int). Every variable also has a Storage Class, which tells the compiler four things:
  1. 1. Scope: Where can this variable be seen/accessed?
  1. 2. Lifetime: How long does the variable exist in memory?
  1. 3. Default Value: If uninitialized, what is its value (0 or garbage)?
  1. 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 auto storage class (the default).
  • Use the static keyword to retain values across function calls.
  • Use extern to share variables across multiple files.
  • Understand the register keyword 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
1234
void myFunc() {
    int x = 10; // This is automatically an 'auto' variable.
    auto int y = 20; // Exact same meaning, rarely written out.
}

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
1234567891011121314
#include <stdio.h>

void counter() {
    static int count = 0; // Initialized ONLY the first time
    count++;
    printf("Count is %d\n", count);
}

int main() {
    counter(); // Count is 1
    counter(); // Count is 2
    counter(); // Count is 3
    return 0;
}

*(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
1
int sharedData = 50; // Defined here

File2.c:

c
1234567
#include <stdio.h>
extern int sharedData; // Tells compiler "sharedData exists somewhere else"

int main() {
    printf("Shared Data: %d\n", sharedData);
    return 0;
}

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
123456
void heavyLoop() {
    register int i;
    for(i = 0; i < 1000000; i++) {
        // Fast execution
    }
}

*(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. extern only *declares*, it does not *define* (allocate memory).

9. Exercises

  1. 1. Write a function with an auto variable and a static variable. Increment both and call the function 5 times to see the difference.
  1. 2. What happens if you try to access a static variable defined in func1() from inside func2()? 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?

Q7. Can you use the & 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 put static in 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 register keyword.

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.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·