Storage class in C

Sakshi Dhameja  7 min read
12 Apr 2023
Intermediate
104 Views

Introduction

Are you eager to gain a better understanding of computer programming? Do you love the challenge of learning a new skill and building complex programs? If so, then an excellent place for you to begin is by exploring the fundamentals of Storage Class in C. In this article, we'll take a closer look at storage class in C and how it influences different aspects of programming from memory management to declaring variables. Understanding storage class can mean the difference between a basic program and one that is robust, reliable, and efficient making it essential knowledge for any aspiring programmer! Dive into this journey with us as explore what lies beneath the surface of storage classes in C.

What is Storage class in C

The fascinating world of programming in C is full of features that allow developers to create versatile, efficient, and powerful applications. Storage classes in C play a pivotal role in determining the scope, visibility, and lifetime of variables and functions within the program. As the users delve deeper into C programming, they will encounter four essential storage classes, each with its specific behavior and functionality. These include automatic, register, static, and external, which provide distinctive ways to handle variable storage and allocation. By mastering these storage classes, the users will possess the ability to optimize their code, ensure the smooth execution of their projects, and tackle real-world programming challenges with grace and expertise. So, embark on the coding journey and unleash the full potential of C programming by understanding and making use of storage classes in C.

Types of Storage class in C

There are four types of storage classes in C, which are
  • Automatic storage class in C
  • Static storage class in c
  • Register storage class in c
  • External storage class in c
Storage ClassesStorage PlaceDefault ValueScopeLifetime
autoRAMGarbage ValueLocalWithin function
externRAMZeroGlobalTill the end of the main program Maybe declared anywhere in the program
staticRAMZeroLocalTill the end of the main program, Retains value between multiple functions call
registerRegisterGarbage ValueLocalWithin function

Automatic storage class in c

Automatic storage class in c is one of the most basic and important aspects of the C programming language. This class enables the user to declare variables without having to specify their type at run time. It also provides a way for c-programmers to declare variables of a specific type, such as int, double, and char. Automatic Storage Class in C is so named because it allows the computer's compiler to automatically assign storage space from a given pool that is available. Automatic Storage Class allows users to write more efficient code by allowing them to access memory more quickly than if each variably needed manual allocation. Automatic Storage can help create organized code by allowing the user to assign certain types of data in a logically appropriate area within a program's overall code base. Automatic Storage Class can thus greatly improve the clarity, efficiency, and effectiveness of a program.

Example

#include <stdio.h> 
int main() 
{ 
int a; //auto 
char b; 
float c; 
printf("%d %c %f",a,b,c); // printing initial default value of automatic variables a, b, and c. 
return 0; 
} 

Output

garbage garbage garbage

Static storage class in c

The static storage class in C is a powerful tool for programmers, allowing them to promote optimal memory management and facilitate efficient program execution. By utilizing the static keyword, programmers can enable variables to retain their state, even if the function they reside in is called multiple times within the course of the program. This feature enhances the code's effectiveness and reliability, as variables can maintain their assigned values throughout the program’s lifetime without needing constant re-initialization. Additionally, it results in a more streamlined and clean code as it restricts variables to a locally confined scope, which can significantly reduce potential conflicts and boost the clarity of the code. Furthermore, by employing the static storage class in C, programmers can vastly increase the performance of their applications and achieve more sophisticated solutions to complex problems.

Example

#include<stdio.h>
static char c;
static int i;
static float f;
static char s[100];
void main ()
{
printf("%d %d %f %s",c,i,f); // the initial default value of c, i, and f will be printed.
}

Output

0 0 0.000000 (null)

Register storage class in c

The register storage class in C provides an exciting opportunity for programmers to optimize the execution time of their code. By utilizing the register keyword while declaring a variable, programmers instruct the compiler to store that variable within the microprocessor's high-speed registers instead of standard memory. This allows for rapid access and manipulation of the variable's data, resulting in more efficient execution of critical code segments. It is important to keep in mind that the usage of register storage class is typically reserved for small, frequently used variables, due to the limited availability of registers in the microprocessor. The register storage class not only enhances the performance of the code but also serves as a valuable tool in the programming arsenal for tailoring the applications to meet the demands of today's fast-paced computing environment.

Example

#include <stdio.h>
int main()
{
register int a; // variable a is allocated memory in the CPU register. The initial default value of a is 0.
printf("%d",a);
}

Output

0

External storage class in c

The external storage class in C is an intriguing feature that allows extending a program's functionality by sharing variables across multiple source files. This increases the level of modularity, allowing programmers to work on different parts of the application simultaneously without disrupting the overall flow. By simply applying the 'extern' keyword, one can use a variable that was originally initialized in another file, thereby promoting the better organization of code and enhanced maintenance capabilities. This powerful aspect of C programming bridges the communication gap between various code segments, empowering developers to create ambitious, complex projects with relative ease and efficiency. In essence, the external storage class elevates the potential of software development by streamlining code integration and collaboration within the programming environment.

Example

#include <stdio.h>
int a;
int main()
{
extern int a; // variable a is defined globally, the memory will not be allocated to a
printf("%d",a);
}

Output

0
Summary
In conclusion, storage class in C is a powerful element of the programming language as it allows control over variables and their data types. It's a fundamental building block of the language that highlights how to adapt your code to meet different program requirements. If you're working with C and want more granular control over your variables, storage class can be a beneficial addition to your coding arsenal. With the help of this article, you should now have a better understanding and appreciation for what storage classes are and the power they bring by controlling variable scope, initial values, and lifetime within programs. So whether you're just starting or an experienced coder, understanding semantic rules associated with variable designation can go a long way toward creating effective and efficient applications. As always, if you have any questions regarding storage classes or any other component of programming in C don't hesitate to reach out! Let's continue exploring Storage Class in C together!
Accept cookies & close this