Different Types of Storage Classes in C [With Examples]

Updated on October 21, 2024

Article Outline

Storage Class is a specifier in C languages. It is one of the important topics of memory management in C language. This variable that we declare in a C program has its scope and life. In this article, we will learn about the various kinds of Storage Classes in C language.

What is Storage Class in C?

In the C programming language, a storage class defines the scope or visibility and lifetime of variables and functions. It specifies where a variable is stored, its initial value, and its life duration within a program. Storage classes help manage variables efficiently, ensuring they are accessible only when necessary and in the appropriate context.

Storage Class in C

  • Scope (Visibility of a variable): The scope of a variable says where the variable is accessible and where it is not in the program. It is determined at compile time only without creating any function call stack in which it is created, which happens at runtime. In simple words, When using a variable out of its scope, the compiler will throw an error.

 

  • Default definition value: We have to declare memory allocation, which does not happen in the declaration, whereas memory gets allocated to the variable in the definition. If the developer does not explicitly initialise it, it gets initialised to some default value according to the storage class.

 

  • Lifetime: The lifetime of a variable defines how long we can access it in the program within its permitted scope.
*Image
Get curriculum highlights, career paths, industry insights and accelerate your technology journey.
Download brochure

Syntax

Here, we will define the syntax of the storage class for a variable.

 

storage_class date_type_of_variable name_of_variable ;

Memory Layout of a C program

Memory Layout of a C program

Let’s see the typical memory architecture in a C program. It can be broken down into 4 parts. Let’s see each part one by one.

 

  • Code Segment: This section contains the executable instructions for the program and is allocated a fixed size according to the length of instruction during the program’s compilation. For example, a programmer writes “Hello World” in C language. It will be stored in the memory in the Code Segment section.

 

  • Static & Global Variables: This section stores all the static and global variables created by the developer in the entire program and allocates fixed memory during compilation.

 

  • Stack: Stack stores the function call at the run time while the developer program is running. It grows dynamically according to the number of function calls made by the user.

 

  • Heap: This memory section has nothing to do with the data structure. This is the program’s free pool of memory, which is used at run time according to the user’s memory needs. It is referred to as a dynamic memory allocation in technical terms.

Summary of Storage Classes in C language

In this section, we will see all the storage in class in tabular form.

 

Storage Class Scope  Lifetime Initial Value Storage Location
Auto This class is declared within a block. This storage class only accesses where it is declared Initialised with garbage value RAM
Register This is declared within the block in which it is defined Same as auto, within the block in which it is declared. It was initialised with a garbage value. Register
Static The within the block, in which it is defined Throughout the main program Zero RAM
Extern It is defined by global scope, unbound by any function It is the same as static till the end of the main program Zero RAM

Uses of C Storage Classes

Let’s see the uses of storage classes in C language.

 

  • It determines the scope, visibility, and lifetime of a variable.
  • We can use more than one variable with the same name.

Types of Storage Class in C

1. Automatic Storage class in C: This is the default storage class for all the variables declared inside a function or a block. Hence, the keyword auto is rarely used while writing programs in the C language. Local variables defined with auto are accessible within the function boundary or the block in which they are defined.

 

            Syntax of auto storage class

auto int max; int min;

All auto variables must be defined inside the block of code. Otherwise, It will throw an error.

 

2. Register Storage Class in C: The register storage class has all the attributes of auto storage. The only difference between auto and register classes is that the register stores the value in the micro-process register, which is faster than variables stored in memory during the program’s runtime. If a free registration is not available, then it will store the value in memory. One thing to remember about the register: We cannot obtain the address of the register variable using the pointer in the C language.

 

            Syntax for register variable definition               

           register int variable ;    

 

3. Static Storage Class in C: Static variables are initialised inside static memory during the program’s compile time and are independent of the function call stack in which they are defined. They live until the end of the program. By default, the static variable is initialised with ‘0’. Developers can also define global static variables at the start of the program, which can be accessed anywhere in the program.

 

4. Extern Storage Class in C and the extern keyword: An extern storage class in C language simply tells the developer that the variable is defined elsewhere and not within the block where it is used. The value assigned to it is in a different block, and this can be overwritten in a different block as well. The main purpose of using external variables is that they can be accessed between two different files that are part of a large program. So, an extern class is nothing but a global variable initialised with legal value in C language.

Storage Class Example

In this section, we will implement the Storage Class example in C language.  The following program uses every storage class.   The following program demonstrates the Storage Class example:

 

Program  

 

#include <stdio.h> extern int externVar; // Function prototypes void autoExample(); void registerExample(); void staticExample(); void externExample(); int externVar = 0; int main() { printf("Demonstrating different storage classes in C:nn"); autoExample(); registerExample(); staticExample(); staticExample(); externExample(); externExample(); return 0; } void autoExample() { auto int num = 10; printf("Auto variable: %dn", num); } void registerExample() { register int counter; for (counter = 0; counter < 3; counter++) { printf("Register variable counter: %dn", counter); } } void staticExample() { static int staticVar = 0; staticVar++; printf("Static variable: %dn", staticVar); } void externExample() { externVar++; printf("Extern variable: %dn", externVar); }

Output 

Demonstrating different storage classes in C: Auto variable: 10 Register variable counter: 0 Register variable counter: 1 Register variable counter: 2 Static variable: 1 Static variable: 2 Extern variable: 1 Extern variable: 2

Advantages and Disadvantages of C Storage Class

There are some advantages and disadvantages of storage class in C language. Let’s describe the advantages and disadvantages of C:

 

Storage Class Advantages Disadvantages
auto It is a default storage class for local variables in C language. Limited to the scope of the block or function. Lifetime is restricted to the block, so values are not retained between function calls.
register Using register storage class variables stored in the CPU registers. It is optimise CPU registers are limited, which may lead to a fallback to the stack. The developer cannot get the address using the pointer variable.
static Local static variables retain their value between function calls. Global variables have file scope, which enhances encapsulation Static storage consumes memory for the entire program duration. Overuse of static storage to higher memory consumption
extern This storage makes the variable globally accessible for all files. Potential for naming conflicts and unintended modification. It makes debugging harder due to dependencies between multiple files.

Difference Between Auto and Static

The following table differentiates the auto and static storage classes:

 

Difference Points Auto Static
Declaration Syntax auto int a = 10 Static int a = 30 ;
Scope Inside Block Inside Block
Initial Value Garbage Value Zero
Lifetime Till the end of the block Till the end of the program

Conclusion

In this article, we learned various storage classes in the C language. These classes are essential for effective memory management, variable scope control, and overall program optimisation. There are four storage classes: auto, register, static, and extern keywords. By Understanding the storage classes ‘register’ for speed optimisation, ‘static’ for persistent local and encapsulated global variables, and ‘extern’ for shared across multiple files. Programmers can write more efficient, maintainable and organised code. Mastering these storage classes enhances the ability to optimise performance, reduce memory footprint, and improve the overall structure and clarity of programs.

FAQs
The different storage classes in C are ‘auto’, ‘register’, ‘static’ and ‘extern’.
The default storage class for local variables in C is ‘auto’.
The ‘register’ storage class suggests that a variable be stored in a CPU register for faster access. It is typically used for frequently accessed variables such as loop counters.
No, you cannot take the address of a ‘register’ variable using the ‘&’ operator because it may be stored in a CPU register rather than in memory.
A static variable in C is a variable that retains its value between function calls when declared locally or has file scope when declared globally.

Updated on October 21, 2024

Link
left dot patternright dot pattern

Programs tailored for your success

Popular

IIT Courses

Management

Data Science

Finance

Technology

Future Tech

Upskill with expert articles

View all
Hero Vired logo
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.
Blogs
Reviews
Events
In the News
About Us
Contact us
Learning Hub
18003093939     ·     hello@herovired.com     ·    Whatsapp
Privacy policy and Terms of use

|

Sitemap

© 2024 Hero Vired. All rights reserved