The features of a variable defined to have a static storage class are as under:
Storage
− Memory.
Default initial value
− Zero.
Storage
− Memory.
Default initial value
− Zero.
Scope
− Local to the block in which the variable is defined.
Life
− Value of the variable persists between different function calls.
Compare the two programs and their output given in Figure 6.3 to understand the difference between the automatic and static storage classes.
− Local to the block in which the variable is defined.
Life
− Value of the variable persists between different function calls.
Compare the two programs and their output given in Figure 6.3 to understand the difference between the automatic and static storage classes.
The programs above consist of two functions main( ) and increment( ). The function increment( ) gets called from main( ) thrice. Each time it increments the value of i and prints it. The only difference in the two programs is that one uses an auto storage class for variable i, whereas the other uses static storage class.Like auto variables, static variables are also local to the block in which they are declared. The difference between them is that static variables don’t disappear when the function is no longer active. Their values persist. If the control comes back to the same function again the static variables have the same values they had last time around.
In the above example, when variable i is auto, each time increment( ) is called it is re-initialized to one. When the function terminates, i vanishes and its new value of 2 is lost. The result: no matter how many times we call increment( ), i is initialized to 1 every time.
On the other hand, if i is static, it is initialized to 1 only once. It is never initialized again. During the first call to increment( ), i is incremented to 2. Because i is static, this value persists. The next time increment( ) is called, i is not re-initialized to 1; on the contrary its old value 2 is still available. This current value of i (i.e. 2) gets printed and then i = i + 1 adds 1 to i to get a value of 3. When increment( ) is called the third time, the current value of i (i.e. 3) gets printed and once again i is incremented. In short, if the storage class is static then the statement static int i = 1 is executed only once, irrespective of how many times the same function is called.
Consider one more program.
main( )
{
int *j ;
int * fun( ) ;
j = fun( ) ;
printf ( "\n%d", *j ) ;
}
int *fun( )
{
In the above example, when variable i is auto, each time increment( ) is called it is re-initialized to one. When the function terminates, i vanishes and its new value of 2 is lost. The result: no matter how many times we call increment( ), i is initialized to 1 every time.
On the other hand, if i is static, it is initialized to 1 only once. It is never initialized again. During the first call to increment( ), i is incremented to 2. Because i is static, this value persists. The next time increment( ) is called, i is not re-initialized to 1; on the contrary its old value 2 is still available. This current value of i (i.e. 2) gets printed and then i = i + 1 adds 1 to i to get a value of 3. When increment( ) is called the third time, the current value of i (i.e. 3) gets printed and once again i is incremented. In short, if the storage class is static then the statement static int i = 1 is executed only once, irrespective of how many times the same function is called.
Consider one more program.
main( )
{
int *j ;
int * fun( ) ;
j = fun( ) ;
printf ( "\n%d", *j ) ;
}
int *fun( )
{
int k = 35 ;
return ( &k ) ;
}
Here we are returning an address of k from fun( ) and collecting it in j. Thus j becomes pointer to k. Then using this pointer we are printing the value of k. This correctly prints out 35. Now try calling any function (even printf( ) ) immediately after the call to fun( ). This time printf( ) prints a garbage value. Why does this happen? In the first case, when the control returned from fun( ) though k went dead it was still left on the stack. We then accessed this value using its address that was collected in j. But when we precede the call to printf( ) by a call to any other function, the stack is now changed, hence we get the garbage value. If we want to get the correct value each time then we must declare k as static. By doing this when the control returns from fun( ), k would not die.
return ( &k ) ;
}
Here we are returning an address of k from fun( ) and collecting it in j. Thus j becomes pointer to k. Then using this pointer we are printing the value of k. This correctly prints out 35. Now try calling any function (even printf( ) ) immediately after the call to fun( ). This time printf( ) prints a garbage value. Why does this happen? In the first case, when the control returned from fun( ) though k went dead it was still left on the stack. We then accessed this value using its address that was collected in j. But when we precede the call to printf( ) by a call to any other function, the stack is now changed, hence we get the garbage value. If we want to get the correct value each time then we must declare k as static. By doing this when the control returns from fun( ), k would not die.
All this having been said, a word of advice—avoid using static variables unless you really need them. Because their values are kept in memory when the variables are not active, which means they take up space in memory that could otherwise be used by other variables.
nice post..
ReplyDeleteMachine Learning Online Training In India
Machine Learning Online Training