Tuesday, 16 May 2017

Storage Classes

Storing the data for execution is the heart of the programming.What actually matters is accessing the stored data.So,lets See how to perform these interesting tasks...

What is scope and life time of variables?


 Scope of a variable is actually the area of our program where we can access our variables.
 life time of variable is the time for which the variable has a valid memory.

What is storage class?


Any variable/function that exists inside the program will have scope and life time.This scope and life time of variables is described through storage class.

we have four storage classes.


1)auto
2)extern
3)static
4)register



auto 


Any variable without specifying any storage class,is considered  as auto. i.e, auto is the default storage class for any variable.
These variables are assigned with garbage value by default when ever they are declared.

Example:


int main()
{ 
 int a;
 printf("%d",a);
 return 0;
}

Output:

garbage value.

static:

 

If a variable is declared as static,then compiler will allocate memory for that variable only once. i.e these variables can hold their values even after they are out of their scope.The scope of static variables is local to the function to which they were defined. how ever global static variables  can be accessed any where in the program. The default values of static variables is 0.
static declaration is very important when we are working with recursion.

without static

 

Example :

main()
{
 functionA();
}
 
functionA()
{
 int c=5;
 if(c-- )
 {
  printf(“%d”,c);
  return functionA();
 }
  else
  return 0;
}

Output:

Segmentation fault/Run time Error.


With static


Example:

void main()
{
 functionA();
}
functionA()
{
 static int c=5;
 if(c-- )
 {
  printf(“%d”,c);
  return functionA();
 }
  else
  return 0;
}

Output:
4 3 2 1 0

register:


When ever a variables is declared as register,then the compiler tries to store these variables in
register of microprocessor,provided these must exist a free register. So declaring a variable as register
is just a request and it is may or may not be granted. The benefit of storing a variable in register can be accessed very fast than the variable stored in the memory.One interesting thing is,we cannot obtain the address of a register variable using pointer.

Example 1:

          

int main()
{
 register int a=5;
 printf("%d",a);
 return 0;
}

Output:

5

Example 2:

int main()
{
 register int a=5;
 printf("%d",a);
 printf("%d",&a);
 return 0;
}

Output:

In function 'main':
error: address of register variable 
'a' requested
printf("%d",&a);


extern

 

extern is used to extend the scope of variable/function.you can define an extern variable else where but
not with in the the block where it is used. All global variables are extern by default.
here is a complete reference for extern.

Example 1:

int main()
{
 extern int i=50;
 printf("%d",i);
 return 0;
}

Output :

compiler error.

 Example 2:

int main()
{ 
 extern int i;
 printf("%d",i);
 return 0;
}
i=50;

Output : 

50

No comments:

Post a Comment