In this C++ tutorial, you will learn about storage classes, types of storage class variables – Automatic, External and Static explained with examples.
Storage classes:
In the context of scope of variables in functions exists the important concept of storage class.
What is Storage Class?
Storage class defined for a variable determines the accessibility and longevity of the variable. The accessibility of the variable relates to the portion of the program that has access to the variable. The longevity of the variable refers to the length of time the variable exists within the program.
Types of Storage Class Variables in C++:
- Automatic
- External
- Static
Automatic:
Variables defined within the function body are called automatic variables. Auto is the keyword used to declare automatic variables. By default and without the use of a keyword, the variables defined inside a function are automatic variables.
For instance:
void exforsys( )
{
auto int x;
auto float y;
...
...
}
is the same as
void exforsys( )
{
int x;
float y; //Automatic Variables
...
...
}
In the above function, the variable x and y are created only when the function exforsys( ) is called. An automatic variable is created only when the function is called. When the function exforsys( ) is called, the variables x and y are allocated memory automatically. When the function exforsys( ) is finished and exits the control transfers to the calling program, the memory allocated for x and y is automatically destroyed. The term automatic variable is used to define the process of memory being allocated and automatically destroyed when a function is called and returned. The scope of the automatic variables is only within the function block within which it is defined. Automatic variable are also called local variables.
External:
External variables are also called global variables. External variables are defined outside any function, memory is set aside once it has been declared and remains until the end of the program. These variables are accessible by any function. This is mainly utilized when a programmer wants to make use of a variable and access the variable among different function calls.
Static:
The static automatic variables, as with local variables, are accessible only within the function in which it is defined. Static automatic variables exist until the program ends in the same manner as external variables. In order to maintain value between function calls, the static variable takes its presence.
For example:
#include <iostream>
using namespace std;
int exforsys(int);
int exforsys2(int);
void main( )
{
int in;
int out;
while(1)
{
cout << "nEnter input value:";
cin>>in;
if (in == 0)
break;
out=exforsys(in);
cout << "nResult : " << out;
out=exforsys2(in);
cout << "nResult2: " << out;
}
cout << "n End of Program " ;
}
int exforsys(int x)
{
static int a=0;
a++;
return(a);
}
int exforsys2(int x)
{
int b=0;
b++;
return(b);
}
In the above program, the static variables a is initialized only once in the beginning of the program. Then the value of the variable is maintained between function calls.
When the program begins, the value of static variable a is initialized to zero. The value of the input in is 3 (which is not equal to zero) and is then passed to the function in variable x. The variable a is incremented thus making a as equal to 1, thus, the return of value from function exforsys( ) for the first time is 1, which is printed in the called function.
It is the same case for the second function, exforsys2(), the variable b which is not declared static, is incremented thus making it equal to 1, and the return value for the first time is 1.
The second time the value of the input in is 7 (which is not equal to zero) and is passed to the function in variable x. The variable a (which is declared as static) has the previous value of 1. This is incremented and the value of a is equal to 2, so the return value is now 2.
Now, for the second function, the variable b which was not declared static, starts from 0 again, making it equal to 1 after incrementing it. Being not declared static, it will always start from 0.
Thus the output of the above program is: