Auto is the default storage class for the local varaibles. Auto is a short form for Automatic.
OutPut:
a is 0
i is 0
In the above code, two integer variables are declared as automatic , in that one is using keyword and another one is without keyword. both variables behaviour is same. They are not inittialised, so default value is garbage value. Here in the output , it is zero. Defualt value is depends on the machine. I am using Unix, so got default value zero. If you use Windows , you may get garbage value. So if you not initialise the variable, result is undefined. So its always better to initialise the variables.
Variable initialization:
OutPut:
a is 20
i is 20
In the above code, two integer variables are declared as automatic , in that one is using keyword and another one is without keyword. both variables behaviour is same.
Scope or visibility sample code:
OutPut:
a is 30
i is 20
a is 20
In the above code, on integer varibale a is declared in a main block, another two variabels a and i declared in the separted block. second variable a and i are limited to this block only. If second variable is not declared in the block, first declared a variable scope comes to this block also.
- Keyword : auto
- Default value : Garbage value
- Memory Location : Main memory
- Scope : Local to the block or function
- Lifetime : Till the application runs or until memory location destroys
int main() { auto int a; int i; printf("a is %x\ni is %d\n",a,i); }
OutPut:
a is 0
i is 0
In the above code, two integer variables are declared as automatic , in that one is using keyword and another one is without keyword. both variables behaviour is same. They are not inittialised, so default value is garbage value. Here in the output , it is zero. Defualt value is depends on the machine. I am using Unix, so got default value zero. If you use Windows , you may get garbage value. So if you not initialise the variable, result is undefined. So its always better to initialise the variables.
Variable initialization:
int main() { auto int a=20; int i=20; printf("a is %x\ni is %d\n",a,i); }
OutPut:
a is 20
i is 20
In the above code, two integer variables are declared as automatic , in that one is using keyword and another one is without keyword. both variables behaviour is same.
Scope or visibility sample code:
int main() { auto int a=20; { int a = 30; // scope is limited to this block int i=20; // scope is limited to this block printf("a is %d\ni is %d\n",a,i); } // we cant access i here // a is first declared variable and not the second declared variable printf("a is %d\n",a); }
OutPut:
a is 30
i is 20
a is 20
In the above code, on integer varibale a is declared in a main block, another two variabels a and i declared in the separted block. second variable a and i are limited to this block only. If second variable is not declared in the block, first declared a variable scope comes to this block also.
No comments:
Post a Comment