Automatic Storage Class
In this tutorial, we will learn how to use the automatic storage class to declare a variable that behaves in a specific way.
In this tutorial, we will learn how to use the automatic storage class to declare a variable that behaves in a specific way.
To make a variable automatic, you can use the keyword “auto” before the variable name. Here’s an example of how to do it:
auto int var;
In the previous tutorial, we learned that each storage class provides information about four important aspects of a variable: storage, default value, scope, and lifetime. The features of a variable with the automatic storage class are as follows:
It’s important to note that the compiler automatically assumes the storage class of a variable as automatic if you don’t specify it explicitly. Therefore, writing “int var;” is the same as writing “auto int var;”
Now, let’s examine each feature in more detail with the help of programming examples.
When we declare an automatic variable, it is assigned a memory location. If we don’t initialize the variable with a specific value, it will contain a random and unpredictable value. Here’s an example program that demonstrates this:
#include <stdio.h>
int main() {
auto int var;
printf("%d", var);
}
Output:
The output of this program will be a garbage value, which means it could be any random number. We cannot predict the value beforehand.
The scope and lifetime of an automatic variable can be understood with the help of the following program:
#include <stdio.h>
int main()
{
auto int var = 10;
{
auto int var = 20;
{
auto int var = 30;
printf("%d ", var);
}
printf("%d ", var);
}
printf("%d", var);
return 0;
}
Now, let’s analyze the output of this program.
In this program, we have three variables with the same name, which may seem unusual because normally we cannot have multiple variables with the same name in a code. However, in this case, it is allowed because each variable is defined within a separate block (indicated by curly braces {}). Each block has its own scope, which means the variables defined inside a block are only visible within that block and its nested blocks (a nested block is a block inside another block). Therefore, the following definitions are legal:
int main()
{
int x = 5;
{
int x = 10;
{
int x = 30;
}
}
}
But NOT the following:
int main()
{
int x = 5;
int x = 10;
int x = 30;
}
Coming back to our example code, the innermost printf()
function has access to all three variables because it lies within all three blocks. However, it prioritizes the variable that is most local (or nearest) to it. Therefore, the innermost printf()
will print the value 30.
As the execution moves to the next printf()
call, the innermost block is completed, and the variable with the value 30 is destroyed. This shows that the scope and lifetime of a variable are limited to a specific block. Now, the second printf()
call has access to only two variables. It will print the value 20, which is the nearest variable.
Similarly, the last printf()
call will print the value 10 because it is the only variable available at that point.
Therefore, the output of the program is
Leave a comment