Wednesday

Storage Classes in C Programming Language


 Storage Classes in C Programming Language

A storage class is an attribute that tells us where the variable would be stored, what will be the initial value of the variable if no value is assigned to that variable, life time of the variable and scope of the variable.

There are four storage classes in C:
1) Automatic storage class
2) Register storage class
3) Static storage class
4) External storage class

Automatic storage class:

The keyword used for Automatic storage class is 'auto'.
The variable declared as auto is stored in the memory.
Default value of that variable is garbage value.
Scope of that variable is local to the block in which the variable is defined.
Variable is alive till the control remains within the block in which the variable id defined.
Example:
#include<stdio.h>
#include<conio.h>
Void main(){
          auto int a;
          printf(“%d”,a)
}
Output:
1285
As seen above, the output is garbage value.

Register storage class:

The keyword used for Register storage class is 'register'.
The variable declared as register is stored in the CPU register.
Default value of that variable is garbage value.
Scope of that variable is local to the block in which the variable is defined.
Variable is alive till the control remains within the block in which the variable id defined.
Main difference between auto and register is that variable declared as auto is stored in memory whereas variable declared as register is stored in CPU register. Since the variable is stored in CPU register, it takes very less time to access that variable. Hence it becomes very time efficient.
It is not necessary that variable declared as register would be stored in CPU registers. The number of CPU registers is limited. If the CPU register is busy doing some other task then variable might act as automatic variable.

Example:
#include<stdio.h>
#include<conio.h>
Void main(){
          register int a;
          printf(“%d”,a)
}
Output:
4587
As seen above, the output is garbage value.

Static storage class:

The keyword used for Static storage class is 'static'.
The variable declared as static is stored in the memory.
Default value of that variable is zero.
Scope of that variable is local to the block in which the variable is defined.
Life of variable persists between different function calls.

External storage class:

The keyword used for External storage class is 'extern'.
The variable declared as static is stored in the memory.
Default value of that variable is zero.
Scope of that variable is global.
Variable is alive as long as the program’s execution doesn’t come to an end.

External variable can be declared outside all the functions or inside function using 'extern' keyword.
Example:
#include<stdio.h>
#include<conio.h>
int a;
Void main(){
          extern int b;
          printf(“%d %d”,a,b)
}
int b=10;
Output:
0 10

Kindly Bookmark this Post using your favorite Bookmarking service:
Technorati Digg This Stumble Stumble Facebook Twitter
YOUR ADSENSE CODE GOES HERE

0 comments:

Post a Comment

Dont Forget for Commets

 

| C programing tutorials © 2009. All Rights Reserved | Template Style by My Blogger Tricks .com | Design by Brian Gardner | Back To Top |