Storage Class in C language

“An investment in knowledge pays the best intrest”

Hello everyone!!!!!!!! Hope you all must be doing well……


Today we are going to discuss storage classes in C language.


Soo let’s start 🙂

A storage class represents the visibility and a location of a variable. It tells from what part of code we can access a variable. A storage class in C is used to describe the following things:

•The variable scope.
• The location where the variable will be stored.
• The initialized value of a variable.
• A lifetime of a variable.
• Who can access a variable?
• Thus a storage class is used to represent the information about a variable.

There are 4 main storage classes in C language.


Let’s discuss one by one.

1)Auto Storage Class :-


• The variables defined using auto storage class are called as local variables.
Auto stands for automatic storage class. A variable is in auto storage class by default if it is not explicitly specified.
• The scope of an auto variable is limited with the particular block only. Once the control goes out of the block, the access is destroyed. This means only the block in which the auto variable is declared can access it.
• A keyword auto is used to define an auto storage class. By default, an auto variable contains a garbage value.
• Example, auto int age;
• Automatic variables are allocated memory automatically at runtime.
• The visibility of the automatic variables is limited to the block in which they are defined.
• The scope of the automatic variables is limited to the block in which they are defined. The automatic variables are initialized to garbage by default.
• The memory assigned to automatic variables gets freed upon exiting from the
block.
• The keyword used for defining automatic variables is auto.
• Every local variable is automatic in C by default.

Example:-

#include
int main( )
{
auto int j = 1;
{
auto int j= 2;
{
auto int j = 3;
printf ( ” %d “, j);
}
printf ( “\t %d”,j);
}
printf( “%d\n”, j);
}

Output:-
3 21

2) Static storage class:-


• The variables defined as static specifier can hold their value between the multiple function calls.
• Static local variables are visible only to the function or the block in which they are defined.
• A same static variable can be declared many times but can be assigned at only one time.
• Default initial value of the static integral variable is 0 otherwise null.
• The visibility of the static global variable is limited to the file in which it has declared.
• The keyword used to define static variable is static.

Example:-

#include
void sum()
{
static int a = 10;
static int b = 24;
printf(“%d %d \n”,a,b);
a++;
b++;
}
void main()
{
int i;
for(i = 0; i< 3; i++)
{
sum(); // The static variables holds their value between multiple function calls.
}
}

Output :-
10 24
11 25
12 26

3) Register storage class:-


• The variables defined as the register is allocated the memory into the CPU registers depending upon the size of the memory remaining in the CPU.
• We can not dereference the register variables, i.e., we can not use &operator for the register variable.
• The access time of the register variables is faster than the automatic variables.The initial default value of the register local variables is 0.
• The register keyword is used for the variable which should be stored in the CPU register. However, it is compilers choice whether or not; the variables can be stored in the register.
• We can store pointers into the register, i.e., a register can store the address of a variable.
• Static variables can not be stored into the register since we can not use more than one storage specifier for the same variable.

Example:-

#include
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

4)Extern Storage Class:-


•Extern stands for external storage class. Extern storage class is used when we have global functions or variables which are shared between two or more files.
• Keyword extern is used to declaring a global variable or function in another file to provide the reference of variable or function which have been already defined in the original file.
• The variables defined using an extern keyword are called as global variables. These variables are accessible throughout the program. Notice that the extern variable cannot be initialized it has already been defined in the original file.

Example:-

#include
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

Soo these are the four main storage classes in C language. I hope you all must have got the concept.
Soo will end this article here 🙂

See you all in next article…….. Till then good bye.. And if you have any doubt please feel free to ask.
Have a wonderful day ahead 🙂

You may also like...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: