The storage class of a variable affects its scope, its lifetime and its location in a memory. The scope of variable determines where a variable is known in a program. The lifetime of variable refers to the length of time that a variable remains available in a program. C++ includes four storage classes:
automatic STORAGE CLASS
When a variable is declared in a function, it becomes an automatic variable. The automatic variable is somtimes called a local variable.
The automatic variable has the following characteristics:
Example | Output |
void test( ); void main() { int num=0; //num1 is explicitly declared as an auto test( ); num++; test( ); num++; test( ); num++; cout <<"main: the value of num is "<<num<< endl; } void test( ) {int num =0; cout << "test: the value of num is " <<num<<endl; num++; } |
test: the value of num is 0 test: the value of num is 0 test: the value of num is 0 main: the value of num is 3 |
static automatic STORAGE CLASS
When a variable is declared in a function with a keyword static, it becomes a static automatic variable. The static automatic variable has the following characteristics:
Example | Output |
void test( ); void main() { int num=10; //num1 is explicitly declared as an auto test( ); num++; test( ); num++; test( ); num++; cout <<"main: the value of num is "<<num<< endl; } void test( ) {static int num =2; // num is a static automatic variable cout << "test: the value of num is " <<num<<endl; num++; } |
test: the value of num is 2 test: the value of num is 3 test: the value of num is 4 main: the value of num is 13 |
external (global) STORAGE CLASS
An extenal or global variable class is declared outside a function. The external storage class variable has the following characteristics:
Example | Example |
int a =1, b=2,c=3; int test( ); void main() { int value; value = test(); cout <<" "<<" value: "<<value<< endl; cout<<"a: "<<a<<endl; cout<<"b: "<<b<<endl; cout<<"c: "<<c<<endl; } int test( ) { int sum; a = a + 2; // a = 1 + 2 =3 b = b + 3; // b = 2 +3 = 5 sum = a + b; //sum =3+ 5 =8 return sum; } |
void test1( ); void test2(); void test3(); void main() { test2 ( ); test1( ); } void test1( ) { cout <<"bye"<<endl; } int a = 5; //a is an external variable. It's available to //the test2( ) and test3( ) functions only. void test2( ) { a = a +10; //a = 5 + 10 = 15 test3( ); } void test 3( ) { a = a + 20; //a = 15 + 20 = 35 cout <<"a: "<<a<<endl; |
Output | Output |
value: 8 a: 3 b: 5 c: 3 |
a: 35 bye |
Example |
void test1( ); void test2(); void test3(); void main() { test2 ( ); //Since the external variable a is not available to the function main( ), but you can still //use it in the main ( ) function by declaring it as extern int x; howerver, the function //test( ) still can not access the variable a. extern int x; cout<< "a: "<<a<<endl; test1( ); } void test1( ) { cout <<"bye"<<endl; } int a = 5; //a is an external variable. It's available to //the test2( ) and test3( ) functions only. void test2( ) { a = a +10; //a = 5 + 10 = 15 test3( ); } void test 3( ) { a = a + 20; //a = 15 + 20 = 35 } |
Output |
a: 35 bye |
Example | |
//file1.cpp int x=2; int y=5; void test1( ); void test2( ); void main( ) { test1( ); test2( ); test3( ); test4( ); } extern int z; //the variable z defined from file2.cpp is now //available to functions test1( ) and test2( ) void test1( ) { int sum1; sum1= x + y + z; //sum=2+5+10=17 cout <<"sum1: "<<sum1<<endl; x=x+10; //x=2+10=12 y=y+20;//y=5+20=25 } void test2( ) { cout <<"x: "<<x<<endl; cout << "y: "<<endl; } |
//file2.cpp void test3( ); void test4( ); int z=10; // z is an external variable extern int x; // the external variable defined from //file1.cpp file is now available to test3( ) and //test4( ) functions void test3( ) { int sum2; sum2=x + z; //sum=12+10=22 cout<<"sum2: "<<sum2; z=z+30; //z=10+30=40 } void test4( ) { cout<<"z: "<<z; } |
Output |
|
sum1: 17 x: 12 y: 25 sum2: 22 z: 40 |
A static external variable class is declared outside a function . The static external variable is defined with a keyword static The characteristics of an static external variable is similar to that of an external variable; however, a static external variable can not be accessed from another file.
Example | |
//file1.cpp int x=2; int y=5; void test1( ); void test2( ); void main( ) { test1( ); test2( ); test3( ); test4( ); } extern int z; //the variable z defined from file2.cpp is now available to //functions test1( ) and test2( ) void test1( ) { int sum1; sum1= x + y + z; //illegal statement you can not use z //in test1( ) because z is a static external variable in //file2.cpp file. sum1 = x+y; // 2 + 5 =7 cout <<"sum1: "<<sum1<<endl; x=x+10; //x=2+10=12 y=y+20;//y=5+20=25 } void test2( ) { cout <<"x: "<<x<<endl; cout << "y: "<<endl; } |
//file2.cpp void test3( ); void test4( ); static int z=10; // z is as tatic external variable extern int x; // the external variable defined from //file1.cpp file is now available to test3( ) and //test4( ) functions void test3( ) { int sum2; sum2=x + z; //sum=12+10=22 cout<<"sum2: "<<sum2; z=z+30; //z=10+30=40 } void test4( ) { cout<<"z: "<<z; } |
Output |
|
sum1: 7 x: 12 y: 25 sum2: 22 z: 40 |
Variables of storage class have the same scope and lifetime as auto variables. The values of register storage class variables, however, will be placed in the machine registers (hardware) rather in memory. Register may be accessed more rapidly than memory, and this strategy can improve the speed and efficiency of programs.