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++
There are four storage classes in c++
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 variable2.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.
Storage | Memory |
Default Initial Value | Garbage Value(Unpredictable value) |
Life | Till the control remains within the function in which it is defined |
Scope | Local 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 */
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 variable2.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.
Storage | Memory |
Default Initial Value | Zero |
Life | static variabl's value persists between different function call. |
Scope | Local 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 */
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 variable2.Value stored in register can be accessed faster than one that is stored in memory.
Storage | CPU Register |
Default Initial Value | Garbage value |
Life | Till the control remains within the function/block in which it is defined. |
Scope | Local 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.
Storage | Memory |
Default Initial Value | Zero |
Life | Till remains to the end of program. |
Scope | Global |
#include<iostream> using namespace std; int x=10;//external variable int main() { extern int x=5; cout<<x; } /* ### Output ### 5 */
Comments
Post a Comment
If you have any doubt. Please comment me know.