Share via


auto Keyword (Storage-Class Specifier)

The auto keyword declares a variable in the automatic storage class.

auto declarator ;

Remarks

Before Visual C++ 2010, the auto keyword declares a variable in the automatic storage class. That is, a variable that has a local lifetime and is visible only in the block that it is declared in. The automatic storage class is the default storage class for block-scoped variables. 

This definition of the auto keyword complies with the original C++ standard. Use the /Zc:auto- compiler option to explicitly select this behavior.

Few programmers use the auto keyword in declarations because all block-scoped objects that are not explicitly declared with another storage class are implicitly automatic. Therefore, the following two declarations are equivalent.

// auto_keyword_storage.cpp
// Compile with /Zc:auto-
int main()
{
   auto int i = 0;    // Variable i is explicitly declared auto.
   int j = 0;         // Variable j is implicitly declared auto.
}

Initialization

This documentation calls a variable that is declared in the automatic storage class an automatic variable. Declarations of automatic variables can include initializers, as discussed in Initializers. An automatic variable is initialized every time it comes in scope if an initializer is provided. Because automatic variables are not initialized by default, you should initialize them when you declare them, or assign initial values to them in the block. The value of an uninitialized automatic variable is undefined.

See Also

Reference

Storage-Class Specifiers

auto Keyword

C++ Keywords

Initializers

Concepts

Initialization