You are on page 1of 19

STORAGE CLASS

INTRODUCTION

We all know C++ is blocked structured language, so we can use the same variable in the C++ program in separate blocks i.e when we declare a variable it is available only to that block of program. The area or block of the C++ program where the variable can be accessed is known as Scope of variable Area or scope of the variable depend where on its storage class i.e where and how it is declared. Storage class of the variable tells the compiler
1.
2. 3. 4.

Storage area of the variable Initial value of the variable Scope of variable Life of the variable i.e how long it would be active

Broadly storage class is classified as

Local variable:-it is visible to the function in which it is declared. Declared inside the function

Global variable :-it is visible to all the functions and it is outside the function. the proper place of declaring of global variable is at the beginning of the main program

TYPES OF STORAGE CLASS


1.

2.
3. 4.

Automatic variable External variable Static variable Register variable

AUTOMATIC VARIABLE
It is a default storage class to be used within a program or file. defined and accessed within the function Local variables is also automatic variables b/c it is automatically created (when function is called) and automatically destroyed (when execution of function is complete) Auto variables are stored on to stack, which provides temporary storage Its default initial value is garbage value.

EXAMPLE
#include<iostream.h> #include<conio.h> void main() { int a=10; void func(); Cout<<a; func(); getch(); } void func() { int a=20; Cout<<a; }

EXTERNAL VARIABLE
The variable that are available to all the functions i.e entire program they can be accessed The scope of the variable is global. Its initial value is zero

EXAMPLE
#include<iostream.h> #include<conio.h> int a; void main() { void func1(),func2(); a=a+1; cout<<a; func1(); func2(); getch(); }

void func1() { a=a+10; cout<<a; } void func2() { a=a+5; cout<<a; }

STATIC VARIABLE
It is a storage similar to auto except that the variable once defined anywhere do not die till program ends the value of the variable will also be retained but will be accessed only by the function declaring it Static storage variable have initial value as zero

EXAMPLE
Void main() { void func(); func(); func(); func(); getch(); } Void func() { static int a=0; a++; cout<<a; }

REGISTER VARIABLE
We can also keep some variables in the cpu register(high speed memory near to the processor) instead of memory. The keyword register tells the compiler that variable is kept on the cpu register, since register is faster than memory access Register has Limited space, so if not space available it will treat variable as auto variables

EXAMPLE
#include<iostream.h> #include<conio.h> void main() { register int m=1; for(;m<=5;m++) { cout<<m; } getch(); }

SUMMARY
STORAGE CLASS Auto External Static Register LIFE TIME Local Global Global Local SCOPE Local(within function) Global (in all function) Local Local

void main()

{ func(); func(); } void func() { auto int i=0; register int j=0; static int k=0; i++; j++; k++; printf( \n %d %d %d, i, j, k); }

You might also like