The Storage class of a variable defines the scope and lifetime of variable. Storage class specifiers control two different properties: storage duration/life time (determines how long a variable can exist) and scope (determines which part of the program can access it).
C++ supports four storage classes:
1) auto
2) register 3) extern 4) static.
The general syntax for specifying the storage
class of a variable is as fallows
<Storage_Class_Specifier> <Data_Type> <Variable_Name> ;
Example:
auto int x;
static int y;
1. Auto Storage Class:
The auto storage class is the default
storage class for all local variables. The scope of an automatic
variable has block scope. The Automatic variable is stored in the program's
stack. They are also called Automatic variable because they come into
life form when scope is entered and automatically go away when the scope ends.
The keyword auto is used, but by default
all local variables are auto, so we don't have to explicitly add keyword auto
before variable declaration. Default value of such variable is garbage.
Example:
auto int x;
2. Register Storage class:
The storage is used if you want faster access to
the variable. The variable will get stored in the CPU register instead of RAM
(memory). The other properties of the register storage are same as of automatic
storage, except the location, where the variable is stored. Here we are just
requesting the compiler to store register variable in CPU's register but the
final decision is up to compiler where it wants to store.
Register variable can be declared in the
following way:
register int x;
3) Extern Storage Class:
The extern storage is used when you want to
access a global variable/object which is defined in another file. We use “extern
class specifier” to specify the compiler that the current object is
defined somewhere else in other file and not in current file.
extern variable can be declared in the following way:
extern int x;
extern
variables can't be initialized. But they can be modified.
Example:
Sub.cpp
int test=100; // assigning value to test
void multiply(int n)
{
test=test*n;
}
Main.cpp
#include<iostream.h>
#include<conio.h>
#include "sub.cpp" // includes the
content of sub.cpp
extern int test; // declaring test
void main()
{
clrscr();
cout<<"test value before
modify="<<test<<"\n"; multiply(5);
cout<<"test value after modify=
"<<test<<"\n"; getch();
}
4. Static Storage Class
Auto is the
default storage class for all local variables. Static is the
default storage class for all global variables.
Static variable has a lifetime (visible) over the
entire program. This means that memory for the static variable is
allocated when the program begins and freed when the program terminates.
General syntax to declare static variable is
static <Data_type> <Variable_name>;
When a static variable is not initialized by the
programmer, then it is automatically initializes to zero (0) when memory is
allocated for them.
If variable is a local static variable, the difference between an auto variable and a static variable is that the static variable when defined within a function is not re-initialized when the function is called repeatedly.
It is initializes just once and further calls of the function
shows the value of static variable. Hence the static variable inside a function
retain its value during various calls.
Storage |
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 |
0 comments:
Post a Comment