Tuesday, June 12, 2012

auto storage class in c

auto:


Automatic variables or auto variables are default storage class of local variable. An auto variable cannot be declared globally. (Why?)
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 examples:
(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.
Question: What will be output of following c code?
#include<stdio.h>
int main(){
    int a=0;
    {
         int a=10;
         printf("%d",a);
         a++;
         {
             a=20;
         }
         {
             printf(" %d",a);
             int a=30; {a++;}
             printf(" %d",a++);
         }
         printf(" %d",a++);
    }
    printf(" %d",a);
    return 0;
}
(3) Scope of auto variable is within the block where it has declared. For example:
(a)
#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 scope only within that block. After the first iteration variable a becomes dead and it looses its incremented value. In second iteration variable a is again declared and initialized and so on.
(b)
#include<stdio.h>
int main(){
    int i=0;
    {
         auto int a=20;
         XYZ:;
         printf("%d",a);
         a++;
         i++;
    }
    if (i<3)
         goto xyz;
    return 0;
}
Output: Compilation error.
Explanation: Variable a which declared inside inner block has scope only within that block. Ones program control comes out of that block variable will be dead. If with the help of goto statement we will go to inside that inner block in the printf statement complier will not known about variable a because it has been destroyed already. Hence complier will show an error message: undefined symbol a. But if you will write goto statement label before the declaration of variable then there is not any problem because variable a will again declared and initialize.
#include<stdio.h>
int main(){
    int i=0;
    {
         XYZ:;
         auto int a=20;
         printf("%d",a);
         a++;
         i++;
    }
    if (i<3)
         goto xyz;
    return 0;
}
Output: 20 20 20
(4) From above example it is clear auto variable initialize each time.

(5)An auto variable gets memory at run time.

No comments:

Post a Comment