Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What are Storage Classes in C?
3.
Use of Storage Class in C
4.
Purpose of storage class in C
5.
Types of Storage Classes in C
5.1.
1. Automatic Storage Class
5.2.
2. Extern Storage Class
5.3.
3. Static Storage Class
5.4.
4. Register Storage Class
6.
Frequently Asked Questions
6.1.
What is the storage class variable?
6.2.
Why storage class memory?
6.3.
Why are storage classes less frequently used?
6.4.
What are storage classes used to provide?
6.5.
Which storage class is faster in C?
6.6.
Why are storage classes important for variables?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

What Is Storage Class In C?

Author Geetika Dua
0 upvote
gp-icon
Data structures & algorithms (Beginner to Intermediate)
Free guided path
13 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction

Have you ever wondered how the memory in computers works?

Or how the variables are stored in the memory?

what is storage class in c

There are some types of storage classes available in every programming language. So today, we will discuss Storage Classes in C.

The variables declared in C programs are different from other languages. We can use the same variable names in the C program in separate blocks. When we declare a variable, it is available only to a specific part or block of the program. The remaining block or other function cannot get access to the variable. The area or block of the C program from where the variable can be accessed is known as the variable’s scope.

There are four scope rules in C:

  • Function: Function scope begins at the opening of the function and ends with its closing. The function has local variables, Global variables, and formal parameters.
     
  • File: These variables are usually declared outside of all the functions and blocks at the top of the program and can be accessed from any portion of the program.
     
  • Block: Scope of an Identifier begins at the opening of the block / ‘{‘ and ends at the end of the block / ‘}.’ Identifiers with block scope are local to their block.
     
  • Function Prototype: Identifiers declared in the function prototype are visible within the prototype. The scope of these variables begins right after the declaration in the function prototype and runs to the end of the Declarations list. These scopes don’t include the function definition but just the function prototype.
     

Now we have just a rough idea about the variable’s scope, but how is this related to storage classes? Let’s explore them.

Also see: C Static Function,Tribonacci Series

What are Storage Classes in C?

A storage class of variables helps us know where the variable would be stored, the Scope of the variable, i.e., in which region of the program the variable’s value is available for us, the life of the variable, i.e., how long the variable would be active in the program(longevity or alive), and the initial value of the variable, if it is not initialised.
 

Storage Classes are used to describe the features of a variable/function. A storage class defines the scope (visibility) and a lifetime of variables or functions within a C Program. These help to trace the existence of a particular variable during the runtime of a program. They precede the type that they modify.

c storage classes
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Use of Storage Class in C

In C, storage classes are used to control the behaviour of variables in terms of their scope, lifetime, and memory allocation.

  • Storage Classes are used for variables that are temporary and needed only within a specific function. They are automatically created when the function is called and destroyed when it exits.
     
  • Static variables are used when you want a variable to retain its value between function calls. They are initialized only once and can be accessed across multiple calls to the same function.
     
  • The register variables are used for fast access to frequently used values. However, the compiler may ignore the request to store a variable in a CPU register if there are too many or if it doesn't provide a performance benefit.
     
  • External variables are useful when you need to share a variable across multiple source files. Define it in one file and declare it as an extern in other files that need to access its value.
    Also read-Bit stuffing program in c

Purpose of storage class in C

Storage classes in C serve several crucial purposes. They allow programmers to tailor the behaviour of variables within their programs. 

For instance, auto variables are temporary and ideal for short-lived calculations within a function, while static variables retain values between function calls. Register variables optimize performance by storing data in CPU registers when possible. Extern variables facilitate sharing data across multiple source files. By using the appropriate storage class, developers can efficiently manage memory usage, control variable access, and enhance program performance, making C a versatile language for various programming tasks.

Types of Storage Classes in C

We have four different storage classes in a C program:

  • auto
  • register
  • static
  • extern
     

Let’s see each storage class briefly and understand it better using different code snippets.

Must Read Passing Arrays to Function in C and Floyd's Triangle in C

1. Automatic Storage Class

Automatic variables/auto are defined inside a function. A variable declared inside a function without a storage class name is by default considered as an auto variable.

Syntax: auto <datatype> variable;


The features of Automatic variables are:

  • Storage : Memory
  • Initial Value : Garbage or Unpredictable
  • Scope : Within Function
  • Lifetime : Till the control remains in the function
     

These variables are created when the function is called and destroyed automatically when the function is exited.

Automatic variables are local to the function in which they are declared. Therefore, these values cannot be accessed by any other function. The keyword used is “auto.”

Code:

#include<stdio.h>
int main( )
{
	auto int a =100; //Auto variable one
	{
		auto int a = 300; //Auto variable two
		{
			auto int a = 500; //Auto variable three
			printf ("a=%d\n",a); //Prints what is within block
		}
		printf ("a=%d\n",a); //Prints what is within block
	}
	printf ("a=%d\n", a); //Prints what is within block
	return 0;
} 


Output:

a=500

a=300

a=100

As Automatic variables are permitted within the block, the values of variables inside a particular block have been printed.

You can also read about the jump statement and Short int in C Programming

2. Extern Storage Class

External variables/extern are also known as global variables. These are declared outside the function, and the values of these variables are available to all the program’s functions.

syntax : extern <datatype> variable (extern is optional as you declare it globally.)


Unlike Local Variables, Global Variables can be accessed by any function in the program. If the same name is given to both the global and local variables, priority is given to the local variable. The keyword “extern” is used to declare these variables.

The features of external variables are:

  • Storage: memory
  • Initial value: zero
  • Scope: Global
  • Lifetime: Till the program comes to an end
     

Code:

#include<stdio.h>
int a=20; //Global variable
int main( )
{
	fun1( );
	fun2( );
	fun3( );
	printf("\n In main function a=%d", a);
	return 0;
}
void fun1( )
{
	printf("\n In fun1 a = %d", a); //prints the global value
}
void fun2( )
{
	int a = 10;
	printf("\n In fun2 a = %d",a); //prints 10
}
void fun3( )
{
	printf("\n In fun3 a = %d", a); //prints global value
}


Output:

 In fun1 a = 20

 In fun2 a = 10

 In fun3 a = 20

 In main function a=20

In this program, local variables and global variables are declared with the same name in fun2( ). In this case, when fun2( ) is called, the local variable “a” of fun2( ) overrides the global variable “a.”

3. Static Storage Class

Static variables/static may be Local or global depending upon where it is declared. For example, it is static global if it is declared outside the function; otherwise, it is static local if declared inside a function block.

Syntax: static <datatype> variable;


A static variable is initialized only once and can never be re-initialised. The value of the static variable persists at each call, and the last change made in the variable remains throughout the program execution. The keyword used to declare these variables is “static.”

The features of a static variable are:

  • Storage: memory
  • Initial value: zero
  • Scope: Local to the block in which the variable is defined
  • Lifetime: persists till the end of program execution
     

Code:

#include<stdio.h>
void incr( )
{
	static int x; //default value is 1
	x=x+1; 
	printf("%d\n", x);
}

int main( )
{
	incr( ); //prints 1
	incr( ); //prints 2
	incr( ); //prints 3
	return 0;
}


Output:

1

2

3

As the static variables store the value, they don’t get initialised to ‘1’ for every function call. So the output increments whenever we call the function from main.

Must Read Static Blocks In Java.

4. Register Storage Class

Instead of storing in memory, variables can also be stored in the register of the CPU. The advantage of storing in registers is that register access is faster than memory access, so frequently accessed variables are kept in registers for faster execution.

Syntax: register int count;


The keyword ‘register’ tells the compiler that the variable list is kept on the CPU registers. If the CPU fails to keep the variables in CPU registers, in that case, the variables are assured as auto and stored in the memory.

Syntax: register <datatype> variable;


Note: CPU registers are limited in number. So, we cannot declare more variables as register variables. The compiler automatically converts the register variables into non-register variables once the limit is reached.

We cannot use the register class for all types of variables. The CPU registers in the microcomputer are 16-bit registers. The data types float and double need space of more than 16 bits. If we define any variable of these types with the register class, no errors will be shown, but the compiler treats them as auto variables.

The features of register variables are:

  • Storage: Registers
  • Initial value: Garbage
  • Scope: Local
  • Lifetime: Until the control remains in that function block
     

Code:

#include<stdio.h>
int main( )
{
	register int i; //declaration of register variable
	for (i=1; i<=5; i++) 
	printf ("%d\n", i); //prints 1,2,3,4,5
	return 0;
}


Output:

1

2

3

4

5

The register variables work only in the particular block, so the value doesn’t change until the block expires.

Must Read what is storage class in c and Decision Making in C

Frequently Asked Questions

What is the storage class variable?

A storage class variable is a programming concept defining a variable's scope and lifetime, like its visibility within functions or throughout the program, and how long it exists in memory.

Why storage class memory?

Storage class memory refers to the type of memory management used for variables, which impacts their lifespan and accessibility. It's crucial for optimizing memory usage and controlling variable behaviour.

Why are storage classes less frequently used?

Register storage classes are less commonly used because they are not guaranteed to be stored in a register. Depending on the available registers and the other variables in the programme, the compiler may decide to store the variable in memory instead.

What are storage classes used to provide?

Storage classes are used to provide information about the scope, lifetime, and linkage of variables and functions. A variable's or function's scope is the area of the programme where it can be used. The lifetime of a variable is the period of time during which it exists. The linkage of a variable or function is its ability to be accessed from other files.

Which storage class is faster in C?

The fastest storage class in C is the register storage class. Variables declared with the register storage class are stored in registers, which are fast memory locations on the CPU. This makes access to these variables much faster.

Why are storage classes important for variables?

Variable storage classes are important because they determine how the compiler will treat them. The scope of a variable determines where it can be used, the lifetime of a variable determines how long it will exist, and the linkage of a variable determines if it can be accessed from other files.

Conclusion

This blog discussed the storage classes and their types, followed by exploring the types of variables in storage classes and analyzing them with code. At last, we have discussed some of the faqs.

Explore more related articles:

You can refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

 

Previous article
​​Difference between C and Embedded C
Next article
Use of Semicolon in Programming languages
Guided path
Free
gridgp-icon
Data structures & algorithms (Beginner to Intermediate)
13 chapters
109+ Problems
gp-badge
Earn badges and level up
Live masterclass