Storage Classes in C++ Explained with Examples

0
Storage Classes in C++ Explained with Examples
Storage Classes in C++ Explained with Examples

📑 Table of Contents

What is Storage Class in CPP

C++ Storage Class: A storage classes in defines the scope of variable, Variable lifetime, default value and storage space in memory of a variable. It also decides the scope of the variable where it can be used. Storage class controls the lifetime of a variable. Storage class also sets the default initial value of a variable.

C language provides different storage classes are auto, register, static, and extern.


Types of Storage Classes in C Plus Plus

  • Automatic Storage Class.
  • Static Storage Class.
  • Register Storage Class.
  • External Storage Class.

Automatic Storage Class:  A Automatic variables are declared inside a function in which they have to used. When the function is called automatic variables are created and destroyed when function is exited. Automatic variables can not be used outside that function in which it is declared it means we can say that it is private member. Automatic variables are also known as local variable. Automatic storage class can be used to auto keyword.

Scope of Automatic Storage Class

Storage Memory
Default Value Automatic default value is garbage.
Value Life The control within the function defined.
Value Scope Local function in which auto variable is defined with in function scope.

Example of Automatic Storage Class

#include<iostream>
using namespace std;
int main(){
auto int a=200;
{
auto int a=300;
{
auto int a=100;
cout<<"Auto Storage Class:"<<a<<endl;
}
cout<<"Auto Storage Class:"<<a<<endl;
}
cout<<"Auto Storage Class:"<<a;
}

============OUTPUT============
Auto Storage Class:100
Auto Storage Class:300
Auto Storage Class:200

Static Storage Class in CPP

Static Storage Class: A Static variables can be used anywhere in the program inside or outside of a function or block. The value of the static variable exists until the end of program. The static variable which is declared inside a function is called internal static variable and and it can not be used outside that function.

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.

Scope of Static Storage Class

StorageMemory
Default ValueStatic default value is 0.
Value LifeStatic variable value between different function call .
Value ScopeLocal function in which static variable is defined with in function scope.

Without Use Static Keyword Example

#include<iostream>
using namespace std;
void static_data(){
int a=0;
cout<<"Static Value is:"<<a<<endl;
a++;

}
int main(){

static_data();
static_data();
static_data();
static_data();
}
============OUTPUT============
Static Value is:0
Static Value is:0
Static Value is:0
Static Value is:0

Explain Static Storage Class with Example

#include<iostream>
using namespace std;
void static_data(){
static int a=0;
cout<<"Static Value is:"<<a<<endl;
a++;

}

int main(){
static_data();
static_data();
static_data();
static_data();
}
============OUTPUT============
Enter Integer Value:56
Integer Value is: 56

Register Storage Class in CPP

Register Storage Class: A register variables is stored in one of the register of system instead of memory. Value stored in register can be accessed faster than one that is stored in memory.

Lifetime of Register Storage Class

StorageMemory
Default ValueRegister default value is Garbage.
Value LifeRegister variable control and remain function block .
Value ScopeLocal function in which register variable is defined with in function scope.


Explain Register Storage Class with an Example

#include<iostream>
using namespace std;
int main(){
register int i;
for(i=1;i<=5;i++){
cout<<"Register Variable:"<<i<<endl;
}
}

============OUTPUT============
Register Variable:1
Register Variable:2
Register Variable:3
Register Variable:4
Register Variable:5

Extern Storage Class in PP

External Storage Class: A external variable that can be used anywhere in the program is called external variable.
External storage class does not create a variable but it informs the compiler of its existence. External storage used to extern keyword.

Lifetime of Extern Storage Class

StorageMemory
Default ValueExternal default value is 0.
Value LifeExternal variable control end of program.
Value ScopeGlobal function in which external variable is global scope.

Example of External Storage Class

#include<iostream>
using namespace std;
int a=200;
void external_data(){
extern int a;
a=100;
cout<<"Local Value inside function:"<<a<<endl;
}
int main(){
cout<<"Before function call:"<<a<<endl;
external_data();
cout<<"After function call:"<<a<<endl;
}

============OUTPUT============
Before function call:200
Local Value inside function:100
After function call:100

Storage Classes or Scope in CPP

Storage Class Storage Start Value Scope Value Value Life
auto stack garbage Within Block End Block
extern Segment Data 0 Global End of Program
static Segment Data 0 Within Block End of Program
register CPU Register Garbage Within Block End Block

Conclusion of Storage Class in C++

Conclusion: This is a storage class in C++. It can be used scope of program and lifetime defines in storage classes.  I can explains storage classes is very simple and easy method  you can easily understand every example and definition. If any problems within storage classes than you can contact me by email or contact us. I can solution provide easy method. 


Post a Comment

0 Comments
Post a Comment (0)
To Top