16066 Storage Classs
-
Upload
er-ashish-baheti -
Category
Documents
-
view
221 -
download
0
Transcript of 16066 Storage Classs
-
7/29/2019 16066 Storage Classs
1/19
STORAGECLASS
-
7/29/2019 16066 Storage Classs
2/19
INTRODUCTION
We all know C++ is blocked structured language, so wecan use the same variable in the C++ program inseparate blocks i.e when we declare a variable it isavailable only to that block of program.
The area or block of the C++ program where thevariable can be accessed is known as Scope ofvariable
Area or scope of the variable depend where on itsstorage class i.e where and how it is declared.
Storage class of the variable tells the compiler1. Storage area of the variable
2. Initial value of the variable
3. Scope of variable
4. Life of the variable i.e how long it would be active
-
7/29/2019 16066 Storage Classs
3/19
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
-
7/29/2019 16066 Storage Classs
4/19
TYPESOFSTORAGECLASS
1. Automatic variable
2. External variable
3. Static variable
4. Register variable
-
7/29/2019 16066 Storage Classs
5/19
AUTOMATICVARIABLE
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 isautomatically 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.
-
7/29/2019 16066 Storage Classs
6/19
-
7/29/2019 16066 Storage Classs
7/19
EXAMPLE
#include
#include
void main()
{
int a=10;
void func();
Cout
-
7/29/2019 16066 Storage Classs
8/19
EXTERNALVARIABLE
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
-
7/29/2019 16066 Storage Classs
9/19
-
7/29/2019 16066 Storage Classs
10/19
EXAMPLE
#include
#include
int a;
void main()
{
void func1(),func2();
a=a+1;
cout
-
7/29/2019 16066 Storage Classs
11/19
void func1()
{
a=a+10;
cout
-
7/29/2019 16066 Storage Classs
12/19
STATICVARIABLE
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
-
7/29/2019 16066 Storage Classs
13/19
-
7/29/2019 16066 Storage Classs
14/19
EXAMPLE
Void main()
{
void func();
func();
func();func();
getch();
}
Void func()
{static int a=0;
a++;
cout
-
7/29/2019 16066 Storage Classs
15/19
REGISTERVARIABLE
We can also keep some variables in the cpu
register(high speed memory near to the processor)
instead of memory.
The keyword registertells 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
-
7/29/2019 16066 Storage Classs
16/19
-
7/29/2019 16066 Storage Classs
17/19
EXAMPLE
#include
#include
void main()
{
register int m=1;
for(;m
-
7/29/2019 16066 Storage Classs
18/19
SUMMARY
STORAGE CLASS LIFE TIME SCOPE
Auto Local Local(within function)
External Global Global (in all function)
Static Global Local
Register Local Local
-
7/29/2019 16066 Storage Classs
19/19
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);
}