Automatic Storage Class in C++
A storage class determines a variable's lifetime, visibility, and scope in C programming. There are four types of storage classes in C: automatic, register, static, and extern. This article will read about the automatic storage class in C.
What is Automatic Storage Class?
In C programming, "automatic" is a storage class that can declare local variables in a block or a function.
If you declare a variable with an automatic storage class, the variable is created when the block containing the declaration is entered and destroyed when the block is exited. This means the variable has a lifetime limited to the block’s scope.
The auto keyword is used to declare a variable with an automatic storage class. However, the auto keyword is optional because it is the default storage class for local variables declared within a block or function.
Properties of Automatic Storage Class
Storage duration: Variables declared with the auto storage class have automatic storage duration, which means that they are created when the program execution enters the block where they are defined, and they are destroyed when the block is exited.
Default storage class: auto is the default storage class for variables declared within a function or a block. Therefore, the auto keyword is often omitted when declaring variables, as it is redundant.
Uninitialized by default: Automatic variables are not initialized by default, meaning they contain garbage values until explicitly initialized.
Scope: Automatic variables have block scope, meaning they can only be accessed within the block where they are defined. If the block is executed, the variable is destroyed, and its memory is freed.
Stack allocation: Automatic variables are typically allocated on the stack, a region of memory used for function call frames and local variables. The stack size is limited, so allocating too many automatic variables or allocating large arrays with an automatic storage class can make stack overflow errors.
Usage of Automatic Storage Class
The automatic storage class is used for variables that have a short lifespan and are only needed within a specific block of code. For example, variables used for loop counters, temporary variables, and function arguments can be declared with an automatic storage class.
The following code snippet demonstrates the usage of the automatic storage class:
#include <stdio.h> void myFunction() { int myVariable = 10; printf("The value of myVariable is: %d\n", myVariable); } int main() { int myVariable = 5; printf("The value of myVariable is: %d\n", myVariable); myFunction(); printf("The value of myVariable is: %d\n", myVariable); return 0; }

- In the code above, we declare two variables with automatic storage class: myVariable in the main() function and myVariable in the myFunction() function. Both variables have the same name but are stored in different memory locations and have different lifetimes and scopes.
- The first printf() statement in the main() function prints the value of the variable declared in the main() function, while the printf() statement in myFunction() prints the value of the variable declared in the myFunction() function. Finally, the last printf() statement in the main() function prints the value of the variable declared in the main() function again.
Conclusion
The automatic storage class is the default storage class in C, and it is utilized for variables that have a short lifespan and are just required inside a particular block of code. By understanding the properties of the automatic storage class, we can utilize it really in our programs to work on their clarity and effectiveness.