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.




19 comments:

nvnaidu said...

thanks for providing this type of sammary

Sunny Kashyap...... said...

#include
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;
}


can u give answer of that question of your blog as well as explanation of the ans...

dinhpq said...

thanks.

kamal raj singh said...

#include

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);
getch();
return 0;
}
OUTPUT: 10,20,31,20,0

read the program in format in which i have written . if any new variable is declared then local variable get more priority inside block {}
. if u have any confusion please let me know.

jyoti said...

plz explain me why the block a=20 is still alive

nikunj said...

how can u initialize a variable in c anywhere?????

Ararsa Lemmessa said...

For #include
int main(){
int i=0;
{
auto int a=20;
XYZ:;
printf("%d",a);
a++;
i++;
}
if (i<3)
goto xyz;
return 0;
}

I got
OUTPUT: 20 20 20

but, for
#include
int main(){
int i=0;
{
XYZ:;
auto int a=20;
printf("%d",a);
a++;
i++;
}
if (i<3)
goto xyz;
return 0;
}

I got

OUTPUT: 20 21 22

I really confused!

nithish kumar raju Raju said...

good concept

Sandeep Uniyal said...

since it is not declared,so no new variable is allocated memory. this is just the previous "a" which is assigned 20.(earlier it has been updated to 11 by a++)

Anonymous said...

10,0,31,0,1

Anonymous said...

need answer for this que..
An auto variable cannot be declared globally. (Why?)

Anonymous said...

It is wrong. It should be "A register variable cannot be declared globally. (Why?)"

Anonymous said...

using assignment operator
eg.int a;
a=10;

Anonymous said...

@Admin
Exaple (b) : In this goto label 'XYZ' is in caps & while calling it ,its in 'xyz' small.

Plz correct it.
and i'm not getting any error, it is printing 20 21 22.
we need some more explanation on it.

ravi jaiswal said...

outpu- 10 20 31 20 0

ravi jaiswal said...

10 20 31 20 0

ravi jaiswal said...

jyoti- because a=20 is assignment its not declaration

Abhishek kumar said...

When I hv written xyz:; before
auto int a=20;
It is showing an error declaration is not allowed here . Plzz reply

ARJUN said...

even I have same question......auto variable cannot declared globally.....reply anyone