What Are Storage Classes in C?
By Rohan Vats
Updated on Apr 22, 2025 | 13 min read | 109.2k views
Share:
For working professionals
For fresh graduates
More
By Rohan Vats
Updated on Apr 22, 2025 | 13 min read | 109.2k views
Share:
Table of Contents
Are you a developer looking to write efficient, optimized, and maintainable code? Then you should enhance your skills with storage classes in C as it is going to be an important skill in 2025. Storage classes determine the scope, lifetime, memory location, and initial value of variables, making them crucial for memory management and performance.
Understanding the four types of storage classes in C—auto, register, static, and extern—can significantly enhance your ability to manage variable accessibility and optimize resource utilization. They are essential for embedded systems, system programming, and applications where performance and memory efficiency are critical.
In this blog, you can learn all about storage classes in C, their types, declaration methods, and best practices. You can develop an in-depth understanding of how to use storage classes to elevate your programming skills effectively.
Storage classes in C define the scope, lifetime, visibility, and memory location of variables. They determine how a variable is allocated and accessed throughout the program, ensuring efficient memory usage and control over variable behaviour.
By understanding storage classes in C Programming, you’ll be able to write optimized, maintainable, and performance-driven code, particularly for system programming and embedded systems.
Key Characteristics of Storage Classes in C:
Importance of Storage Classes in C:
In the next section, you can learn about the various types of storage classes in C programming.
In C programming, storage classes define how variables are stored, accessed, and maintained in memory. The four primary storage classes are automatic, register, static, and extern, each serving a unique purpose in managing variable behaviour.
This section explores each storage class, highlighting its characteristics and providing practical examples to illustrate its behavior.
The automatic storage class is the default for local variables defined within a function or block. These variables are created when the block is entered and destroyed when it exits, making them temporary and local in scope.
#include <stdio.h>
void exampleFunction() {
auto int num = 10; // Automatic storage class (default)
printf("Inside function, num = %d\n", num);
}
int main() {
exampleFunction();
// Variable num is destroyed after function ends
return 0;
}
Output:
Inside function, num = 10
Practical Note:
Automatic variables are ideal for temporary data within functions, as they are automatically managed by the compiler, reducing memory overhead. However, they cannot retain values between function calls.
Also Read: High-Level Programming Languages: Key Concepts Explained
Let’s now have a look at the Register Storage Class.
The register storage class requests that a variable be stored in a CPU register for faster access, rather than in RAM. However, this is only a suggestion, and the compiler may ignore it if registers are unavailable.
#include <stdio.h>
int main() {
register int count = 5; // Register storage class
// printf("%p", &count); // Error: Cannot take address of register variable
printf("Register variable count = %d\n", count);
return 0;
}
Output:
Register variable count = 5
Practical Note:
The register keyword is rarely used in modern compilers, as they optimize variable placement automatically. It’s most relevant for performance-critical applications, but overuse may lead to register exhaustion.
Also Read: Coding vs Programming: Difference Between Coding and Programming
Now, you’ll look at the characteristic features of the Static Storage Class.
The static storage class ensures a variable persists for the program’s entire lifetime, retaining its value between function calls. It can be applied to both local and global variables, with different effects.
#include <stdio.h>
void counter() {
static int count = 0; // Static local variable
count++;
printf("Count = %d\n", count);
}
int main() {
counter(); // First call
counter(); // Second call
counter(); // Third call
return 0;
}
Output:
Count = 1
Count = 2
Count = 3
Practical Note:
Static variables are useful for maintaining state in functions (e.g., counting invocations) or restricting global variables to a single file for better modularity. However, they increase memory usage since they persist for the program’s duration.
Also Read: Skills to Become a Full-Stack Developer in 2025
Finally, let’s explore the traits of the extern storage class.
The extern storage class enables variables or functions defined in one file to be accessed in another, facilitating data sharing in multi-file programs. It declares a variable without allocating storage, relying on a definition elsewhere.
Example Program (Multi-File):
File1.c:
#include <stdio.h>
extern int sharedVar; // Declaration of extern variable
void printSharedVar() {
printf("Shared variable = %d\n", sharedVar);
}
File2.c:
#include <stdio.h>
int sharedVar = 100; // Definition of extern variable
int main() {
printSharedVar();
return 0;
}
Output:
Shared variable = 100
Practical Note:
The extern keyword is essential for modular programming, allowing large projects to share data efficiently. Ensure the variable is defined exactly once to avoid linker errors.
Also Read: Software Developer Roles and Responsibilities in 2024
Now, let’s have a look at the common ways to declare storage classes in C.
Storage classes in C are declared using specific keywords like auto, register, static, and extern. Each storage class has its unique syntax and usage, which helps define the variable’s scope, lifetime, and visibility. Below is a step-by-step guide to declaring these storage classes or variables in C.
Syntax for Auto Variables:
auto type variable_name;
Example:
auto int num;
Usage of Auto Variables:
#include <stdio.h>
void exampleFunction() {
auto int num = 5; // Auto variable
printf("Auto variable num = %d\n", num);
}
int main() {
exampleFunction();
return 0;
}
Output:
Auto variable num = 5
Also Read: C Tutorial for Beginners
In the next step, you can learn about how to declare a register variable.
Syntax for Register Variables:
register type variable_name;
Example:
register int count;
Usage of Register Variables:
#include <stdio.h>
int main() {
register int count;
for (count = 0; count < 5; count++) {
printf("Register variable count = %d\n", count);
}
return 0;
}
Output:
Register variable count = 0
Register variable count = 1
Register variable count = 2
Register variable count = 3
Register variable count = 4
Now, let’s see the best way to declare a static variable.
Syntax for Static Variables:
static type variable_name;
Example:
static int counter = 0;
Usage of Static Variables:
#include <stdio.h>
void countFunction() {
static int counter = 0; // Static variable
counter++;
printf("Counter = %d\n", counter);
}
int main() {
countFunction(); // First call
countFunction(); // Second call
countFunction(); // Third call
return 0;
}
Output:
Counter = 1
Counter = 2
Counter = 3
Finally, let’s check the steps to declare an extern variable.
Syntax for Extern Variables:
extern type variable_name;
Example:
extern int globalVar;
Usage of Extern Variables:
File1.c:
#include <stdio.h>
extern int globalVar; // Declaration of extern variable
void printGlobalVar() {
printf("Global Variable = %d\n", globalVar);
}
File2.c:
#include <stdio.h>
int globalVar = 42; // Definition of extern variable
int main() {
printGlobalVar();
return 0;
}
Output:
Global Variable = 42
In the next section, you can see what happens when no storage class specifier is declared.
upGrad’s Exclusive Software and Tech Webinar for you –
SAAS Business – What is So Different?
When no storage class specifier is explicitly declared in C, variables and functions follow default behaviours based on their location within the code. The default storage class determines the variable’s or function’s scope, lifetime, and visibility in the program.
Declaration Context | Default Storage Class | Behaviour |
Variables inside a function | auto | Local variables are created when the block is entered and destroyed when it is exited. |
Functions declared globally | extern | Functions are globally accessible by default and can be used in other files. |
Variables outside a function | static | Global variables are accessible throughout the program unless marked as static for file scope. |
Now, let’s have a look at the best practices for using Storage Classes in C.
Using storage classes effectively is essential for writing efficient, maintainable, and optimized C programs. Here are best practices for when and how to use each storage class in different scenarios.
Storage Class | When to Use | Example Scenarios |
Auto | Use for local variables that are used only within a function or block. | Temporary calculations in a function, such as summing values or performing intermediate steps. |
Register | Use for frequently accessed variables that need faster access through CPU registers. | Loop counters or frequently modified variables in intensive operations. |
Static | Use for variables that need to retain their values across function calls but remain scoped locally. | Maintaining a counter or flag across multiple function invocations. |
Extern | Use for sharing variables or functions between different files in a multi-file program. | Global configurations or data shared across multiple modules, like logging or settings. |
Next, you’ll learn about the key differences between defining and declaring Storage Classes in C programming.
In C programming, defining and declaring storage classes are distinct operations that serve different purposes. While declaration introduces a variable or function to the program without allocating memory, definition allocates memory and optionally initializes the variable or function.
Understanding the difference between these concepts is crucial for managing memory allocation, initialization, and variable accessibility in programs.
Aspect | Defining | Declaring |
Purpose | Allocates memory for the variable or function and optionally initializes it. | Introduces the variable or function to the program without allocating memory. |
Memory Allocation | Memory is allocated when a variable is defined. | No memory is allocated during a declaration. |
Initialization | Variables can be initialized during definition. | Variables cannot be initialized; they only specify the type. |
Scope of Use | A defined variable can be used within its scope immediately. | A declared variable needs to be defined elsewhere before it can be used. |
Usage Context | Typically used in the same file where the variable or function is implemented. | Used for sharing variables or functions across files, especially with extern. |
Examples | int x = 10; allocates memory and assigns value 10. | extern int x; informs the compiler that x is defined elsewhere. |
As a C programmer or developer, it is essential that you have a complete understanding of C storage classes for your projects. This can enhance the outcome of your projects and improve your value among your employers. upGrad offers numerous C programming language courses that can help you to develop the skills to work with C storage classes.
Here are some upGrad courses that will help you to develop complete knowledge about storage classes in C:
Understanding storage classes in C is essential for writing efficient and maintainable code. By knowing what the storage classes in C are—namely, auto, register, static, and extern—you gain better control over variable scope, lifetime, and memory usage. These types of storage classes in C help define how and where variables are stored, accessed, and modified during program execution.
Using the right storage class specifiers in C ensures clarity and performance, especially in large-scale applications. Mastering these concepts is a fundamental step for every C programmer aiming to build optimized and well-structured code in 2025 and beyond.
Are you looking for some professional career guidance to help you with your career plans? Then make sure that you sign up for upGrad’s free career counseling. These sessions can help you to make wise decisions for your career goals.
Boost your career with our popular Software Engineering courses, offering hands-on training and expert guidance to turn you into a skilled software developer.
Master in-demand Software Development skills like coding, system design, DevOps, and agile methodologies to excel in today’s competitive tech industry.
Stay informed with our widely-read Software Development articles, covering everything from coding techniques to the latest advancements in software engineering.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources