Concept of Storage Classes In C

What are Storage Classes ?

Storage classes in c programming are used to describe about the features of a variable/functions. 
These features basically include scope , life time , default value, storage area of variable/functions in c.

Scope : It means the area of code where a variable is accessible.

Life Time : It means the time duration during runtime till which our variable is live or can be used. In this time duration doesn't means the actual time it related with function life where it is used for that duration.

Default Value : It is the value which is assigned to variable initially when we declare it.

Storage Area : It means where our variable data is stored whether it is in register or hard disk,etc.

Types of Storage Classes

Auto 

* Any variable in c programming is auto variable by default. So, there is no need to use the keyword auto.
 
* if variable is not initialized , by default auto variable contains garbage value.

* auto variable is stored in stack.

* syntax : auto datatype variable_name;

* Example : auto int x;

* It is recreated and reinitialized for every function call.

Consider following code :

void abc()
{
int a=10;
++a;
printf("%d",a);
}
void main()
{
abc();
abc();
abc();
}

Output  : 11 11 11

The above code best explained that for every function call variable are recreated and reinitialized again and again.

Consider this code to understand auto :

void main()
{
int a=10,b=20;
{
int b=30;
printf("%d %d",a,b);
}
printf("%d %d",a,b);
}

Output : 10 30 10 20

Consider one more code :

void main()
{
int a=20,b=2=,c=15,d=22;
{
int a=25,c=25,b=21;
{
int d=40,b=17;
printf("%d %d %d %d\n",a,b,c,d);
}
printf("%d %d %d %d\n",a,b,c,d);
}
printf("%d %d %d %d\n",a,b,c,d);
}

Output : 25 17 25 40

                25 21 25 22

                20 20 15 22

These above code best explained about the scope and lifetime of a variable.

Static

* It is very much different from auto. In thios we use keyword 'static'.

* By default it is initialized to 0.

* It has function block scope.

* It has program lifetime.

* It is stored into static area of RAM.

* Syntax : static datatype variable_name;

* Example : static int a;

* It is created only once and persistence previous stage value from the destruction of several function call (that means it retained previous value for different function call.).

* Static variable preserve the value of last used in their scope. So, we can say that they initialise only once and exist till the termination of program.

* Thus, no new memory is allocated because they are not redeclared.

Consider following code :

void abc()
{
static int a=10;
++a;
printf("%d",a);
}
void main()
{
abc();
abc();
abc();
}

Output : 11 12 13

So, here in this code you can see difference. when we are running previous same code with variable as static it is giving different result. I hope this point is clear to you now.

Register

* To declare register variable with register class we use following syntax : register datatype variable;

*  Example : register int a;
 
* The keyword register is not a comment. it is just a request to the compiler that to store the variable into register instead of RAM(Random Access Memory).

* If there are free register available the variable stores into the register otherwise it will stored in the RAM(Random Access Memory). 

* It has block scope and function lifetime.

* It is initialized with garbage value as default value.

* Advantage of using register storage class is that it has  faster access and disadvantage is that we have limited number of registers. 

External (Global)

* Variable which are declared outside of all function blocks are global. By default global variables are initialized to 0.

* Global variables are accessible in all the function block provided that there is no local variable with the same name of global variable.

Consider this code : 

#include<stdio.h> 
int a; //global variable
main()
{
int b,a,i;  //local variables 
printf("%d%d",a,b);
}

Output : hvgeXX864jm khc7654b   // output will be an two garbage values

* It has program lifetime and program scope.

Consider the following code :

int a=100;
main()
{
printf("%d".a);
}
func1()
{
printf("%d".a);
}
func2()
{
printf("%d".a);
}

This code illustrate that a single global variable can be used in different functions.

Have a look at this code :
  
void abc()
{
g++;
}

int g;
#include<stdio.h>
void main()
{
g++;
abc();
printf("%d",g);
}

This code will give compile time error.

To overcome this ,we assure the compiler for the reference of global variable using the keyword extern.

Correct Code :

#include<stdio.h>
void abc()
{
extern int g; // line 4
++g;
}
int g;
void main()
{
++g;
abc();
printf("%d",g);
}

Output : 2

So, we can observe that if global variable is declared at place below where we are using it the we need to mention it in that function by using extern keyword as we can see in line 4.

This is all about Storage classes. 

Look at my YouTube Channel for different technology related videos.





                    





             




Comments

Popular posts from this blog

Coding tips for Developers

Master Data Structure & Algorithms in Python

How to Start with Coding: A Beginner's Guide