Automatic storage class is default storage class i.e. if there are nothing to add storage class in variable then by default it would be automatic storage class.
The features of a variable defined to have an automatic storage class are as under:
Scope and life of an automatic variable is illustrated in the following program:
/*program to shows the life and scope of automatic variable*/
#include<stdio.h>
int main()
{
auto int i=1;
{
auto int i=2;
{
auto int i=3;
printf("\n%d",i);
}
printf("\n%d",i);
}
printf("\n%d",i);
return 0;
}
OUTPUT:-
3
2
1
In above program, compiler treats the three i's as totally different variables, since they are defined in different block. Once the control comes out of the innermost block, the variable i with value 3 is lost, and hence the i in the second printf() refers to i with value 2. Similarly, when the control comes out of the next innermost block, the third printf() refers to the i with value 1. Hence it is clear that scope of of i is local to the block in which it is defined.
Following program shows how an automatic storage class variable is declared, and what happen if variable is not initialise i.e. garbage value.
#include<stdio.h>
int main()
{
auto int i,j;
printf("\ni=%d \nj=%d",i,j);
return 0;
}
OUTPUT:-
i=54
j=3564
Where, 54 and 3564 are garbage values of i and j. When you run this program, you may get different values, since garbage values are unpredictable.
You might also like to read:
The features of a variable defined to have an automatic storage class are as under:
Storage | Memory |
Keyword | auto |
Default initial value | Garbage value i.e. an unpredictable value |
Scope | Local to the block, in which the variable is defined |
Life | Till the control remains within the block in which the variable is defined |
Scope and life of an automatic variable is illustrated in the following program:
/*program to shows the life and scope of automatic variable*/
#include<stdio.h>
int main()
{
auto int i=1;
{
auto int i=2;
{
auto int i=3;
printf("\n%d",i);
}
printf("\n%d",i);
}
printf("\n%d",i);
return 0;
}
OUTPUT:-
3
2
1
In above program, compiler treats the three i's as totally different variables, since they are defined in different block. Once the control comes out of the innermost block, the variable i with value 3 is lost, and hence the i in the second printf() refers to i with value 2. Similarly, when the control comes out of the next innermost block, the third printf() refers to the i with value 1. Hence it is clear that scope of of i is local to the block in which it is defined.
Following program shows how an automatic storage class variable is declared, and what happen if variable is not initialise i.e. garbage value.
#include<stdio.h>
int main()
{
auto int i,j;
printf("\ni=%d \nj=%d",i,j);
return 0;
}
OUTPUT:-
i=54
j=3564
Where, 54 and 3564 are garbage values of i and j. When you run this program, you may get different values, since garbage values are unpredictable.
You might also like to read:
No comments:
Post a Comment