Storage class is used to define the lifetime and visibility or scope of a variable and function within a C++ program.There
are five types of storage classes in C++. They are,
The extern
variable is visible to all the programs. extern keyword is used to declare external variables. They
are visible throughout the program and its lifetime is same as the lifetime of the program where it is declared.
Syntax
1. Automatic
2. External
3. Static
4. Register
5. Mutable
Storage
class
|
Keyword
|
Lifetime
|
Visibility
|
Initial
Value
|
Automatic
|
auto
|
Function
Block
|
Local
|
Garbage
|
External
|
extern
|
Whole
Program
|
Global
|
Zero
|
Static
|
static
|
Whole
Program
|
Local
|
Zero
|
Register
|
register
|
Function
Block
|
Local
|
Garbage
|
Mutable
|
mutable
|
Class
|
Local
|
Garbage
|
Automatic Storage Class
Automatic storage class assigns a variable to its default storage
type. Auto keyword is used
to declare automatic variables. However, if a variable is declared without any
keyword inside a function, it is
automatic by default.
Syntax
Datatype variable_name= value;
Or
auto datatype variable_name= value;
Or
auto datatype variable_name= value;
Example
auto int
x;
float y =5.6; //by default ,storage class is auto
Register Storage Class
The register variable allocates memory in register than RAM. Its size is same of register size. It has a faster access than other variables.
It is recommended to use register variable only for quick access such as in counter.
Syntax
register datatype variable_name=value;
Example:
register int x;
Example 1:
#include<iostream>
using namespace std;
int z; //global variable which initially holds 0 and it can access anywhere in the program
void sample_function()
float y =5.6; //by default ,storage class is auto
Register Storage Class
The register variable allocates memory in register than RAM. Its size is same of register size. It has a faster access than other variables.
It is recommended to use register variable only for quick access such as in counter.
Syntax
register datatype variable_name=value;
Example:
register int x;
Example 1:
#include<iostream>
using namespace std;
int z; //global variable which initially holds 0 and it can access anywhere in the program
void sample_function()
{
static int x; //static variable which initially holds 0
register int y; //register variable
x=7;
y=x*3;
cout<<"Inside
sample_function"<<endl;
cout<<"x =
"<<x<<endl;
cout<<"y =
"<<y<<endl;
cout<<"z =
"<<z<<endl;
}
int
main()
{
int a;
//automatic variable
z=89;
a=50;
sample_function();
cout<<"Inside
main"<<endl;
cout<<"a =
"<<a<<endl;
cout<<"z =
"<<z<<endl;
sample_function();
return 0;
}
Output:
Inside sample_function
x = 7
y = 21
z = 89
Inside main
a = 50
y = 21
z = 89
Static Storage Class:
The
static variable is initialized only once and exists till the end of a program.
It retains its value between multiple functions call. The static variable has the default value 0 which is provided by the compiler.
Syntax
Syntax
static datatype variable_name=value;
Example
static int x = 10;
Example 1:
#include <iostream>
using namespace std;
void display()
{
static int x = 0;
x++;
cout <<"x is:"<< x << endl;
}
int main()
{
display();
display();
display();
return 0;
}
Output:
x is:1
x is:2
x is:3
External Storage Class
The extern
variable is visible to all the programs. extern keyword is used to declare external variables. They
are visible throughout the program and its lifetime is same as the lifetime of the program where it is declared.
Syntax
extern datatype variable_name;
Example,
extern float x;
Example
1:
#include<iostream>
using namespace std;
int x=5;
void add(int y)
{
int c=x+y;
cout<<"sum
is:"<<c<<endl;
}
extern int x;
int main()
{
add(6);
return 0;
}
Output:
sum is: 11
No comments:
Post a Comment