- Automatic storage class is default storage class. All variables declared are of type Auto by default.
- Automatic variables are allocated space in the variable on the stack and To declare a variable automatic storage class auto specified.
- Automatic storage class variable are declare by ‘ auto ‘ class like as below the example.
Example :– auto int i;
- When declare a variable, if the omitted variable storage class is automatic in c programming language.
Example :– int i;
- In c programming language , When declare an automatic variable initialization to be made explicit initial value is undefined.
Features :
- The scope of an automatic variable is for the local and block, or any block within the block in c programming language.
- Value of automatic variable storage in memory.
- The lifetime of this automatic variable exists as long as Control remains in the block.
- Default initial Value of this automatic variable is Garbage.
Now let’s see the syntax,example and output of the automatic storage class.
Syntax :
auto data_type variable_name; |
Example 1 :
1 | #include<stdio.h> |
2 | #include<conio.h> |
3 | |
4 | void addition(); |
5 | |
6 | void main() |
7 | { |
8 | addition(); |
9 | addition(); |
10 | } |
11 | |
12 | void addition() |
13 | { |
14 | auto int p = 0 ; |
15 | printf ( “%d \n”, p ) ; |
16 | p++; |
17 | } |
Output :
0
0
Example 2 :
void main() |
{ |
auto n = 10 ; |
{ |
auto n = 20 ; |
printf(“Num : %d”,n); |
} |
printf(“Num : %d”,n); |
} |
Output :
Num : 10
Num : 20
Here, In example-2 Two variables are declared in different blocks, so they are treated as different variables.