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 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 variable x and y is 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

are accessible only within the function in which it is defined. cout<”\nResult:”<<out. b=b+x.process of memory being allocated and automatically destroyed when a function is called and returned. the value of static variable a and b is initialized to zero. The value of the input in is 5 (which is not equal to zero) and is then passed . This is mainly utilized when a programmer wants to make use of a variable and access the variable among different function calls. while(in!=0) { cout<<”Enter input value:”. Static automatic variables exist until the program ends in the same manner as externalvariables. void main( ) { int in. } In the above program. These variablesare accessible by any function. } int exforsys(int x) { static int a=0. the static variables a and b are initialized only once in the beginning of theprogram.out. For example: #include <iostream. the static variable takes its presence.h> int exforsys(int). out=exforsys(in). External: External variables are also called global variables. static int b=0. Automatic variable are also called localvariables. The scope of the automatic variables is only within the function block within which it is defined. return(b/a). } cout<”\n End of Program”<<out. Static: The static automatic variables. a++. When the program begins. cin>>in. In order to maintain value between function calls. memory is set aside once it has been declared and remains until the end of the program. as with local variables. Then the value of the variables is maintained between function calls. External variables are defined outside any function.

The variable a is incremented thus making a as equal to 1. This is incremented and the value of a is equal to 2. Variable b becomes equal to 5 and thus. which is printed in the called function. the return of value from function exforsys( ) for the first time is 5. . 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 the function in variable x. The value of b is maintained from the previous statement as 5 and new value of b now is b=5+7 = 12 and thus. The variable a (which is declared as static) has the previous value of 1. the return value from the function is 12/2=6 which is printed in the called function.

such variables are called as global variables. auto register static external Storage Class Storage Storage Storage Class Class Class The auto Storage Class The keyword ‘auto’ is used to refer automatic variables. such variables are called as local variables. The scope of thevariable is determined by its place of declaration. The storage class also specifies the scope of the variable. 4. And it is storage class that determines where to store the subsequent variable. C provides four storage classes: 1. Generally a variable can store either inmemory or one of the CPU registers. 3. If we declare a variable inside a function then this variable can be accessed inside the same function only. Automatic variable is declared as: auto data_type variable_name.StorageClassspecifiers You should know that before using any variable we have to explicitly or implicitly mention its data type. If we declare a variable outside all the functions then this variable can be accessed from anywhere in the program. . The portion of a program where a variable can be accessed is called as the scope of that variable. 2. There is no need to declare constants.

{ auto int i. cout<<“Entersecondnumber=”. j as automatic float variables and k as automatic character variables then the declaration section looks like this: Auto Auto inti. Program – str. . The default initial value of a automatic variable is a garbage value. We can also declare an automatic variable at the beginning of a block. // #include voidmain() { auto int i. cin>>i. automatic variables are stored in memory.If you want to declare i as automatic integer variables.cpp illustrates this concept. Program – str. The automaticvariables are local to a function in which these variables are defined. It means that automatic variables are created when the function(that defines the auto variables) is called and destroyed when that function terminates.cpp cout<<“\nEnterfirstnumber=”. floatj. cin>>i. auto char k. It means that each invocation of the statement block in which the auto variable is defined gets a first copy with its own memory space and with reinitialization.

The value of the variable is lost when the control comes out of the block in which the variable is defined. By default the function parameters are auto unless you declare them to be in the register . the output will remain same. } cout<<“\nFirst } I=”<<i. The output of this program str is as: Enter Enter Enter Third Second First first second third number number number = = = = = = 11 12 13 13 12 11 In this program the value of I is different for each block because they are defined in different blocks. cout<<“Enterthirdnumber=”.{ auto inti. It means that if storage class of a variable is not mentioned then it is supposed to be auto by default unless the program declares them otherwise. You will be surprised to know that if you omit keyword ‘auto’ in the above program. The life ofautomatic variable exists till the control remains within block. } cout<<“\nSecond I=”<<i. cin>>i. cout<<“\nThird I=”<<i.

When a reference is made to access it. the register variables are local to a function. Register variable is declared as: register data_type variable_name. .storage class. cin>>i. Enteranyintegervalue=”.cpp #include voidmain() { register cout<<“\n int i. Like automatic variables. the default initial value of register variable is garbage value. Normally any variable is stored in memory. The register storage Class Register variables are stored inside CPU registers in order to provide fast access. // Program – str1. And like auto variables. it is accessed from memory and then it is stored into CPU register. On the other hand you can take the address of an auto variable. The keyword ‘register’ is used to refer register variables. The only difference between an auto variable and a register variable is that you can not take the address of register variable because hardware registers on most computers do not have memory addresses.

In other words we can say that when we declare a variable as a register variable then it is a request to the compiler.cpp is as: Enter You any have integer value entered = 10 =10 While using register storage class. Another main point to note is that register storage class is restricted to integer variables only because CPU registers are 16 – bit registers & each integer occupies 2 bytes. . External variables are declared outside all functions. The output of this program str1. The reason is simple that these variables. Thus the following statement Register float j. The address restriction applies even when the compiler ignores the request and puts the variable in addressable memory. due to any reason. The compiler can ignore this request. The external Storage Class External variables are also referred as global variables. the variables are treated as automatic variables.cout<<“You } have entered=”<<i. one should remember that if the available registers are not enough. j and k. are treated as automatic variables. register float k. would not result in any compiler error.

first(). the external variables are also stored in memory. the default value of external variable is zero. . Therefore the life of external variable remains present throughout the program and is destroyed only when the program terminates. Like automatic variables. remember that external variables are not restricted to any function locally. cout<<"\nThird X="<<x. x=15. second(). int x. But unlike automatic variables.cpp #include void first(). voidsecond(). voidmain() { extern int x.An external variable is declared as: extern data_type variable_name. cout<<"\nFirstX="<<x. cout<<"\nSecond X="<<x. Instead the value of global variable is available to each and every function of the program. Watch carefully the output of the following program: // Program – str2. While using external variables.

} second() first() The output of this program is as…. . // Program – str3. float y.cpp #include void first().} void { x+=5. On the other hand if the variable is declared inside a function then only those functions which have the extern declaration of the variable can reference the variable. That’s why even following program would work correctly. Here one main point to note that use of an external outside all the function is optional. If the variable is declared outside all functions then every function in the program can reference this external variable. X X X = = = 15 20 10 An external variable declaration may be inside or outside the function that references it. } void { x-=10.

cout << “\nSecond Y = ” << y. . } The output of this program is as: Local Y = 27. The static Storage Class There are two types of static variables: 1.void main() { y = 20. cout << “\nFirst Y = ” << y. } void first() { y = y+10. 2.25 Thus if there is any conflict between the global and local variable.5 Global Y = 20.45.25. first(). Static local variables (internal static variable) Static global variables (external static variable) Static local variables are declared inside a function by using a keyword ‘static’ as: static datatype variable_name. the local variable wins the race.

void main() { example(). The value of static local variable would not be destroyed even the function terminates. } The output of this program is as: .cpp #include void example(). float y = 10.25. But remember that the static local variable can be accessed within its own function.45. example().25. example(). x = x+0. y = y+0. } void example() { static float x = 20. cout << “\n X = “ << x << “\tY = ” << y. Following program illustrates this concept: // Program – str4.25.A static local variable is initialized only once when the very first call to the function (that defines it) occurs.

x is declared as static variable whereas y is declared as automatic variable.X = 20. Therefore when function example() is called first time.25 Y = 10.75 Y = 10. Like external variable.25 once whereas y is initialized each time we call function example(). a static global variable is declared outside any function. But it is slightly different than ordinary external variable. x is initialized to 20.45 In function example(). And any file (program) can access ordinary external variable.45 X = 20.45 X = 20. Now let us summarize the different storage classes: . whereas a static global variable can be only accessed in the file (program) it is defined.5 Y = 10.

Sign up to vote on this title
UsefulNot useful