The variable declared in the pgm will have certain storage class. The storage classes that are used in c++ one namely auto, static, register and extern.
1) Auto and static storage class:-
Consider the following pair of pgms
void main ( ) void main ( )
{ int i ; { int i ;
void Fun ( ); void fun ( );
For (i = 1;i<=5; i ++) for (i =1; i<=5; i + +)
fun ( ); fun( );
} }
void fun ( ) void fun ( )
{ {
auto int x = 1 ; static int x = 1 ;
cout << x << ‘\t’ ; cout << x << ‘\t’ ;
x++ ; x++ ;
} }
Explanation for Pgm – I :-
In pgm I the storage class of x is auto. When the function fun ( ) called for the first time then the compiler will allocated space of 2 bytes for x and will initialize it with the value 1. The allocation will take place automatically.
The value of ‘x’ i.e. 1 will be printed and then it will be incremented 2. The Function call will then deallocate the ‘x’ along with its value automatically and the control will return back to the main ( ).
The function fun ( ) will be called again. The compiler will then re-allocate the ‘x’ automatically and will re-initialize its value with 1. The value of ‘x’ i.e 1 will be printed and then x will become 2. The function will be finished and then the ‘x’ will be deallocated automatically along with its value. The process will be repeated and the o/p will be
1 1 1 1 1
Explanation for Pgm – II:-
In pgm – 2 the storage class of ‘x’ is static. When the function fun ( ) is called for the first time then the compiler will allocate space of 2 bytes For ‘x’ and will initialize it with the value 1.
The value of ‘x’ i.e. 1 will be printed and then it will be incremented to 2. The function call will then be finished.
However the ‘x’ will not be deallocated from the memory because its storage class is static. The control will then return back to the main ( ).
The function fun ( ) will be called again. The Function will not re-initialize the value of ‘x’ by 1. This is because the concept is initialization while declaration. Whereas the ‘x’ is already declared and it is there in the memory. Hence the existing value of ‘x’ i.e 2 will be printed and then the ‘x’ i.e 2 will be printed and then the ‘x’ will be incremented to 3 and the function call gets finished. The process will be repeated and the O/P will be,
1 2 3 4 5
Points to Note:-
1) In both the above pgm, the storage class of the variable ‘i’ was auto by default.
2) Both the auto and static storage class variables are stored in primary memory of computer (RAM).
3) The life span of the auto-variable is from the time at which the function containing that variable is called to the time at which that function call gets finished.
4) The life span of the static variable is from the time at which the function containing that variable is called to the time at which the main function i.e. the pgm execution gets finished.
3.Register Storage Class:-
1) When the storage class of the variable is given as register then it will be stored in one of the general purpose registers within the processor. If none of the processors registers is free then the variable will be stored in primary memory just like auto and static variable.
2) Consider the Following example.
Register int x;
If any register within the processor is free then the variable ‘x’ will be stored within the register otherwise it will be stored in primary memory and then it will just act as variable with auto storage class. The register storage class will be used with the variables that require frequent calculations. Since these variables stored within the preprocessor only, those will be accessed much faster and thus the execution time will reduce.
4.Extern Storage Class:-
1) Consider the following example.
# include
int x=123 ;
Void main ( )
{
int y=45 ;
cout << x << ‘ ‘<< y << ‘\n’ ;
}
In the above pgm, the ‘y’ is a local variable of main ( ) and its storage class is auto by default. The variable ‘X’ is external variable, whose storage class is static by default. In addition to that the storage class of variable ‘x’ is extern. This is because it is defined external to the function. The output of the pgm will be 123 45.
* SCOPE OF INDENTIFIERS / SCOPE OF VARIABLES:
The Scope of the variable or identifier PS nothing but the area in which that variable or identifier can be used.
Consider the following example
# include
int x:
void main ( )
extern int q ;
void abc ( ),def ( ), pqr ( ) ;
. . . .
. . .
} int z ;
void abc ( )
{
int p;
. . . .
} int q ;
void def ( )
{
int r ;
. . .
. . . .
}
void pqr ( )
{ int s ;
. . .
. . . .
}
The following table shows the details of all possible variables used in the above pgm.
Variable Storage class Scope
name and type
x extern main ( ), abc ( ), def ( ), pqr ( )
y auto and local main ( )
z extern abc ( ), def ( ), pqr ( )
p auto and local abc ( )
q extern def ( ), pqr ( ), main( )
r auto and local def ( )
s auto and local pqr ( )
* RECURSION:
1) Recursion is the process in which the problem is specified in terms of itself.
2) To implement recursion the function should call itself. The function calling itself is called as recursive function.
3) Every invoke of the recursive function will give the partial solution and will call the next invoke of itself to get the further solution.
4) There must be some condition by which the recursion process will be stopped, otherwise it can lead to an infinite process.
5) When recursion is used then all the partial solutions must be combined to obtain the final solution.
6) For e.g. factorial of 3 can be described as follows.
Fact (3)
3 * Fact (2)
2 * Fact (1)
1 x Fact (o)