auto Storage Class
The auto storage class is the default class of all the variables declared inside a block. The auto stands for automatic and all the local variables that are declared in a block automatically belong to this class.
Properties of auto Storage Class Objects
- Scope: Local
- Default Value: Garbage Value
- Memory Location: RAM
- Lifetime: Till the end of its scope
Example of auto Storage Class
C++
// C++ Program to illustrate the auto storage class // variables #include <iostream> using namespace std; void autoStorageClass() { cout << "Demonstrating auto class\n" ; // Declaring an auto variable int a = 32; float b = 3.2; char * c = "w3wiki" ; char d = 'G' ; // printing the auto variables cout << a << " \n" ; cout << b << " \n" ; cout << c << " \n" ; cout << d << " \n" ; } int main() { // To demonstrate auto Storage Class autoStorageClass(); return 0; } |
Demonstrating auto class 32 3.2 w3wiki G
Note: Earlier in C++, we could use the auto keyword to declare the auto variables explicitly but after C++11, the meaning of auto keyword is changed and we could no longer use it to define the auto variables.
Storage Classes in C++ with Examples
C++ Storage Classes are used to describe the characteristics of a variable/function. It determines the lifetime, visibility, default value, and storage location which helps us to trace the existence of a particular variable during the runtime of a program. Storage class specifiers are used to specify the storage class for a variable.
Syntax
To specify the storage class for a variable, the following syntax is to be followed:
storage_class var_data_type var_name;
C++ uses 6 storage classes, which are as follows:
- auto Storage Class
- register Storage Class
- extern Storage Class
- static Storage Class
- mutable Storage Class
- thread_local Storage Class
Below is a detailed explanation of each storage class:
Contact Us