The features of a variable defined to have an automatic storage class are as under:
|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*/
auto int i=1;
auto int i=2;
auto int i=3;
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.
auto int i,j;
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: