Automatic storage class
A variable declared in the function without storage class name, by default is an auto variable. These functions are declared on the stack. The scope of the variable is local to the block in which they are defined. Auto variables are safety i.e. they cannot be accessed directly by other functions. The scope of the automatic storage class is as follows:
S.No.
|
Features of variable
|
Meaning
|
1.
|
Storage location
|
Memory
|
2.
|
Initial or default value
|
A system generated or unpredictable value, which is often known as garbage value.
|
3.
|
Scope of variable
|
Local to the block in which the variable is defined
|
4.
|
Life of variable
|
Till the control remains within the block in which the variable is defined.
|
Example of Automatic storage class
#include<stdio.h>
#include<conio.h>
void main()
{
auto int num=1;
{
auto int num=2;
{
auto int num=3;
printf("\t%d",num);
}
printf("\t%d",num);
}
printf("\t%d",num);
}
Output: 3 2 1
Explanation: When you run this program you get output from the innermost block to the outermost block because process is from innermost to outermost. Once the control comes out of the innermost block the variable num with value 3 is lost and hence the value of the second printf () is printed and so on.
#include<stdio.h>
#include<conio.h>
void main()
{
int num1, num2;
printf("\t %d %d",num1,num2);
}
Explanation: When you run this program you may get different values, since system generates some unpredictable values. So make sure that when you are declaring auto variable that you initialize the automatic variable properly otherwise program will give unexpected results.