Presentation is loading. Please wait.

Presentation is loading. Please wait.

Storage class.

Similar presentations


Presentation on theme: "Storage class."— Presentation transcript:

1 Storage class

2 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 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

3 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

4 Types of storage class Automatic variable External variable
Static variable Register variable

5 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.

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

7 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

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

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

10 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

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

12 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

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

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


Download ppt "Storage class."

Similar presentations


Ads by Google