Storage class in c++

Storage class

A storage classes in c++ defines the scope,liftime,default initial value and storage space of a variable.
There are four storage classes in c++
1.automatic.
2.static.
3.register.
4.external.

               Automatic: storage class

1.Automatic variables are declared inside a function in which they have to used.
2.When the function is called automatic variables are created and destroyed when function is exited.
3.Automatic variables can not be used outside that function in which it is declared it means we can say that it is private member.
4.Automatic variables are also known as local variable.
5.auto keyword is used to declare automatic type variable.

Features of automatic variable
StorageMemory
Default Initial ValueGarbage Value(Unpredictable value)
LifeTill the control remains within the function in which it is defined
ScopeLocal to the function in which auto variable is defined


                     Example


#include<iostream>
using namespace std;
int main()
{
 auto int x=5;
 {
   auto int x=3;
    {
   auto int x=1;
   cout<<x<<endl;
    }
    cout<<x<<endl;
 }
 cout<<x<<endl;
}
/*
### Output ###
1
3
5
*/
Here the value of innermost x is 1,out of this block value is 3 and out of this block is 5.

                   
Static storage class

1.Static variables can be used anywhere in the program inside or outside of a funtion or block.
2.The value of the static variable exists until the end of program.
3.The static variable which is declared inside a function is called internal static variable and and it can not be used outside that function.
4.The static variable which is declared outside a function is called external static variable and and it can be used in all the function of that program.

Features of static variable
StorageMemory
Default Initial ValueZero
Lifestatic variabl's value persists between different function call.
ScopeLocal to the function in which static variable is defined

Example

#include<iostream>
using namespace std;
void Demo()
{
 static int x=0;
 cout<<x<<endl;
 x++;
}
int main()
{
 Demo();//calling
 Demo();
 Demo();
 Demo();
}
/*
### Output ###
0
1
2
3
*/
Here we can see that Demo function is called four times and each time value is incremented by one.


Register:storage class

1.register variables is stored in one of the register of system ,instead of memory.
2.Value stored in register can be accessed faster than one that is stored in memory.

Features of register variable
StorageCPU Register
Default Initial ValueGarbage value
LifeTill the control remains within the function/block in which it is defined.
ScopeLocal to the function in which variable is defined

Example

#include<iostream>
using namespace std;
int main()
{
register int x,y=20,z=30;
x=y+z;
cout<<"Add="<<x;
}
/*
### Output ###
Add=50
*/

External storage class

1.Variable that can be used anywhere in the program is called external variable.
2.External storage class does not create a variable,but it informs the compiler of its existence.
3.extern keyword is used to declare external variable.

Features of external variable
StorageMemory
Default Initial ValueZero
LifeTill remains to the end of program.
ScopeGlobal

    Example      
#include<iostream>
using namespace std;
int x=10;//external variable
int main()
{
   extern int x=5;
   cout<<x;
}
/*
### Output ###
5
*/

Comments

Popular posts from this blog

Function in C++ with example & declaration, initialization function in c++

Operators of c++