What Is Storage Classes In C With Examples

Hello friends, in today’s article we are going to talking about Storage Classes in C Language.

Today we will learn in detail about what is storage classes in C? And What are the types of storage classes in C Language?

So without wasting time let’s understand what is Storage Classes in C?

Storage Classes In C
Storage Classes In C

What is Storage Classes in C

Storage classes are used in C language to describe the life-time, scope, default value, and storage location of a variable.

Here above I have used some words like – life-time, scope, default value, and storage location

Let’s understand what is the meaning of these?

Life -Time -:

Here life-time refers to the life of a variable. The amount of time a variable stays in a program is called the lifetime of that variable.

The life time of a variable depends on its declaration. We can easily tell the lifetime of a variable by looking at the storage class.

Scope-:

The scope of a variable means that after declaring a variable, where that variable can be used in the whole program. The scope of the variable tells where a variable can be used in the program.

Generally in C language, when we create a variable, the scope of that variable is also limited to that block only in which where we have declared i. 

In c language, if we declare a variable in one block then it cannot accessible by another block.

Default Value or Initial Value -:

When we do not assign or store any value while declaring the variable, then the value which is stored in those variables by default is called Default Value or Initial Value.

Strorage Area or Memroy Location -:

In C language, when we create a variable, then the memory that the variable gets during the execution of the program, that memory is found in RAM or it is found in the register.

Well it depends on the storage class. By looking at the storage class, we can find out in which location the variable will get memory in the program.

Syntax -:

storage_class data_type variable_name;

Types of Storage Classes

There are four types of Storage Classes in C Language:-

  1. Automatic
  2. External
  3. Static
  4. Register

1. Automatic Storage Class

In C language, whenever we create a variable inside a block or function, then it is by default, an Automatic Storage Class variable.

The auto keyword is used while declaring the automatic variable. However, whenever we create a variable in the C language, it is automatic by default.

The variable declared with the auto keyword gets memory in RAM automatically and the scope of this variable is limited only to the block or function in which it is declared.

By declaring this variable in one function, we cannot use it in any other function.

The life of an automatic variable remains as long as the block or function in which the automatic variable is declared is running. As soon as the function execution is over, the automatic variable is also destroyed.

When no value is initialized while declaring a variable, then garbage value is stored in such variable by default.

Syntax -:

auto data_type variable_name;

Example -:

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

Explanation -:

see in the above example, In this, we have created three variables named a, b, and c, in which we have created a variable using auto keyword and we have created b and c variable without auto keyword.

No value is initialized in the a and c variables. We have initialized the b variable with ‘a’.

Since in C language the variables created without the auto keyword are also automatic storage class variables, so we do not have to write the auto keyword separately to create an automatic variable and hence we have created the b and c variables without the auto keyword.

When we execute this program, the result comes like this.

Output -:

0 a 0.000000

Since we have not initialized a and c variables while declaring, so garbage value is printed in the result.

2. External Storage Class

Before understanding the External storage class, it is important to explain to you what is a global variable?

Global variables are variables whose declaration is outside of the function, we can use such variables in our program anywhere, anytime, whenever.

The scope of a global variable is not limited to any block or function, we can use it anywhere in our program. Any function can use and change the value of this variable.

External variable is a kind of global variable. We can use them in different source files. 

External variable is a kind of global variable. We can use them in different source files. The scope of an extern variable is global. And just as a global variable has a lifetime, its life time also extends to the execution of the entire program.

This variable is declared with the Extern keyword. External variable can be declared multiple times but its initialization happens only once.

Syntax -:

extern data_type variable_name;

If the extern variable is of integer type, then by default 0 is stored in it and if the extern variable is not of integer type then the initial value in this is null.

If extern variable is used in a program then it gets memory in RAM. The extern variable is mainly used to increase the visibility of the variable.

Example -:

#include<stdio.h>
extern int var = 0;
int main(void)
{
var = 10;
return 0;
}

3. Static Storage Class

Static variable is created with the help of Static keyword. Static keyword is used at the time of variable declaration to create a static variable.

Its life is till the execution of the entire program, which means as long as the program is executing, the static variable will remain there and as soon as the execution of the program is over, the static variable will also be destroyed.

The scope of a static variable depends on its declaration. If we declare a static variable inside a function or block, then it is called a local static variable and if the static variable is declared outside the function, then it is called a global static variable.

The scope of a local static variable is limited to that function only. We can use this variable only in that function in which it is declared, we cannot use it in any other function.

The scope of the Global Static variable is global, we can use this variable in any function, anywhere in the whole program.

While declaring the value of static variable by default is zero whereas in the normal variable by default garbage value is there.

Note -: When a static variable is used in a function, the number of times that function is called is used with the increased value of the static variable. When a static variable is created, the variable gets memory in RAM.

Syntax -:

static data_type variable_name;

Example -:

#include <stdio.h>
void func(void); //function declaration
static int count = 5; //static global variable
 
main()
{
   while(count--) {
      func();
   }
   return 0;
}
void func( void ) //function definition
 {
  static int i = 5; // local static variable *
   i++;

   printf("i is %d and count is %d\n", i, count);
}

Output -:

i is 6 and count is 4
i is 7 and count is 3
i is 8 and count is 2
i is 9 and count is 1
i is 10 and count is 0

When a static variable is used in a function, the number of times that function is called is used in the function along with the increased value of the static variable. As in this program right now static variable is used which is being used with increased value which you can see in the output.

4. Register Storage Class

We use Register Storage Class when we have to store any variable of our program in the CPU register instead of storing it in RAM. We also do this so that we can access the variable quickly. This increases the speed of the program.

We declare the variable of Register Storage Class by using register keyword.

The access time of register variable is faster than the automatic variable because it is stored in the register so that the CPU can access the data stored in it faster.

When we declare a variable with the register keyword, then that variable gets memory in the register instead of memory in RAM. But it does not mean that by creating a variable with the register keyword, the variable will get memory in the register itself. It depends on the compiler.

With the help of register keyword, we create variables of only integer and character type.

If we create a variable from double or float data type and use register keyword in it, even then the variable of double and float data type will not get memory in the register. They will get memory only in RAM. So we can make any variable as register variable if we want, but where will it get memory, it depends on the compiler.

The scope of register variable is limited to that block or function in which it is declared, we cannot use it in another function by declaring it in one function.

The life time of register memory is equal to the life of the function in which it is created. As soon as the function ends, the register variable is also destroyed.

The register variable by default contains garbage value.

Syntax -:

register data_type variable_name;

Example -:

#include <stdio.h>
void main()
{
register int a = 6; //register variable

printf("%d",a);

// printing initial default value of register variables a.
}

Note -:

In this program, we have created a variable named a, with the help of register keyword, which will get memory in the register or not, it depends on the compiler because the register in the computer is limited if the register is empty in the computer then a variable Memory will be available in register and if the register is not empty then variable gets memory in RAM .

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 the function

Important point about Storage Classes in C Language

Storage classes are used for efficient use of memory and to increase the speed of the program, below I am going to tell you some important points that when and where we should use these storage classes.

  • The static storage class should be used when we want the value of  variable to be used with its incremented value every time in different function calls.
  • We should use register storage class when we want the variable to get memory in register instead of getting memory in RAM. So that the time taken by the cpu to access the variable is reduced and the speed of the program increases.
  • External or global storage class should be used when we need a variable that we can use with any function anywhere in our program.
  • If we do not create a variable for any of the above purposes, then we should use the automatic storage class variable.

Conclusion

Friends, I hope that after reading this article you know very well, what is storage classes in C? And What are the types of storage classes in C Language?

If you want a complete tutorial of C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.

If you liked this post, then do not forget to share this post with your friends so that they can also get information about Storage Classes in C Language

If you still have any questions or doubts related to Storage Classes in C, then tell me in the comment below, I will answer all your questions and you can contact us for more information.

Subscribe to this website newsletter to get information related to this new technology, Programming Language, Coding, C Language, C++Python Course, Java Tutorial.

Jeetu Sahu is A Web Developer | Computer Engineer | Passionate about Coding, Competitive Programming, and Blogging

Leave a Comment