auto storage class
Automatic variable or auto variables are default storage class of local variables. auto keyword is used to declear auto variable.
Syntax
auto data_type var_name;
or
data_type var_name;
Declearation of auto variable
auto int time;
or
int time;
auto variable must be declear locally or within the curly bracket { }.
Example
int sum()
{
auto int sum;
int first;
int second;
return first+second;
}
In this above example. sum, first and second are auto variable.
Mostly programmers do not use auto keyword to declear auto varibale because local variables are considered "auto" by default.
Properties of auto variable:
- default storage class for local variable.
- Visiblity/lifetime of an auto variable is limited within the block where it has declared.
- Scope of an auto variable is limited within the block where it has declared.
- auto variables gets memory at run time.
- auto variable gets memory in stack
- Default initial value of an auto variable will be garbage
- Auto variables are destroyed after the function block is executed.
Guess the output #1
#include <stdio.h>
int main()
{
int score = 10;
printf("score: %d\n", score);
{
score = 20;
printf("score: %d\n", score);
}
printf("score: %d\n", score);
return 0;
}
Compiling this code produces following output, click on below icon to see the output.
score: 10
score: 20
score: 20
Explanation: None.
Guess the output #2
#include <stdio.h>
{
auto int a;
}
int main()
{
int num;
return 0;
}
Compiling this code produces following output, click on below icon to see the output.
auto_3.c:3:1: error: expected identifier or ‘(’ before ‘{’ token
3 | {
| ^
Explanation: None.
Guess the output #3
#include <stdio.h>
auto int a;
int main()
{
printf("Value of a is %d \n", a);
return 0;
}
Compiling this code produces following output, click on below icon to see the output.
auto_4.c:3:10: error: file-scope declaration of ‘a’ specifies ‘auto’
3 | auto int a;
|
Explanation: None.
©2023-2024 rculock.com