Automatic variables or auto variables are default storage class of local variable. An auto variable cannot be declared globally.
Properties of auto storage class
1 Default initial value of auto variable is garbage.
For example :
#include<stdio.h>
int main()
{
int i;
auto char c;
float f;
printf("%d %c %f",i,c,f);
return 0;
}
Output: Garbage Garbage Garbage
2. Visibility of auto variable is within the block where it has declared.
For example:
(a)
#include<stdio.h>
int main()
{
int a = 10;
{
int a = 20;
printf("%d",a);
}
printf("%d",a);
return 0;
}
Output: 20 10
Explanation:
Visibility of variable a which has declared inside inner has block only within that block.
(b)
#include<stdio.h>
int main()
{
{
int a = 20;
printf("%d",a);
}
printf("%d",a); // 'a' is not visible here
return 0;
}
Output: Compilation error
Explanation:
Visibility of variable a which has declared inside inner block has only within that block.
3. Scope of 'auto' variable is within the block where it has declared.
For Example:
#include<stdio.h>
int main()
{
int i;
for(i=0;i<4;i++)
{
int a = 20;
printf("%d",a);
a++
}
return 0;
}
Output: 20 20 20 20
Explanation: Variable declared inside the for loop block has only within that block.
After the first iteration variable a becomes dead and it looses it's incremented value. In second iteration variable 'a' is again declared and initialize and so on.
4. From above example it is clear 'auto' variable initialize each time.
5. An 'auto' vriable gets memory at run time.
0 comments:
Post a Comment