Share via


auto Keyword

The auto storage-class specifier declares an automatic variable, a variable with a local lifetime. It is the default storage-class specifier for block-scoped variable declarations.

auto declarator ;

Remarks

An auto variable is visible only in the block in which it is declared. Declarations of auto variables can include initializers, as discussed in Initialization. Since variables with auto storage class are not initialized automatically, you should either explicitly initialize them when you declare them, or assign initial values to them in statements within the block. The values of uninitialized auto variables are undefined. (A local variable of auto or register storage class is initialized each time it comes in scope if an initializer is given.)

An internal static variable (a static variable with local or block scope) can be initialized with the address of any external or static item, but not with the address of another auto item, because the address of an auto item is not a constant.

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

// auto_keyword.cpp
int main()
{
   auto int i = 0;    // Explicitly declared as auto.
   int j = 0;    // Implicitly auto.
}

See Also

Concepts

Storage-Class Specifiers

C++ Keywords

Using extern to Specify Linkage

register Keyword

Static (C++)