auto used fortype inference, seeC++11 § Type inference.Incomputer programming, anautomatic variable is a localvariable which is allocated and deallocated automatically when program flow enters and leaves the variable's scope. Thescope is the lexical context, particularly the function or block in which a variable is defined. Local data is typically (in most languages) invisible outside the function or lexical context where it is defined. Local data is also invisible and inaccessible to acalled function,[note 1] but is not deallocated, coming back in scope as theexecution thread returns to the caller.
Automatic local variables primarily applies to recursivelexically scoped languages.[note 2] Automatic local variables are normallyallocated in the stack frame of the procedure in which they are declared.[note 3] This was originally done to achievere-entrancy and allowingrecursion,[note 4] a consideration that still applies today. The concept of automatic variables in recursive (andnested) functions in a lexically scoped language was introduced to the wider audience withALGOL in the late 1950s, and further popularized by its many descendants.
The termlocal variable is usually synonymous with automatic variable, since these are the same thing in many programming languages, but local is more general – most local variables are automatic local variables, butstatic local variables also exist, notably in C. For a static local variable, the allocation is static (the lifetime is the entire program execution), not automatic, but it is only in scope during the execution of the function.
In C and C++ (pre-C++11), variables declared withauto are calledautomatic variables.
All variables declared within ablock of code are automatic by default. An uninitialized automatic variable has anundefined value until it is assigned a valid value of its type.[1] The storage-class specifierauto can be added to these variable declarations as well, but as they are all automatic by default, this is entirely redundant and rarely done.
In C, using the storage classregister is a hint to the compiler to cache the variable in a processor register. Other than not allowing the address-of operator (&) to be used on the variable or any of its subcomponents, the compiler is free to ignore the hint.[2]
InC++, the constructor of automatic variables is called when the execution reaches the place of declaration. The destructor is called when it reaches the end of the given program block (program blocks are surrounded by curly brackets). This feature is often used to manage resource allocation and deallocation, like opening and then automatically closing files or freeing up memory, calledResource Acquisition Is Initialization (RAII).
Since C++11, C++ allows variables to be declared with theauto type specifier,[3] but this means that the variable's type isinferred, and does not refer to the scope of the variable. C adopted type inference for theauto type specifier as ofC23.
In Java, automatic variables are calledlocal variables.
Similar to C and C++, but there is noauto orregister keyword. However, the Java compiler will not allow the usage of a not-explicitly-initialized local variable and will give a compilation error (unlike C and C++ where the compiler will usually only give a warning). The Java standard demands that every local variable must be explicitly initialized before being used.[4] This differs from instance variables, which are implicitly initialized with default values (which are0 for numbers andnull for objects).
In Perl, automatic variables are calledlexical,my orprivate variables.
In Perl, local variables are declared using themy operator. Uninitialized scalars will have the valueundef; uninitialized arrays or hashes will be().[5]
Perl also has alocal operator that does not create automatic variables,[6] instead giving global (package) variables a temporary value, which isdynamically scoped to the enclosing block. When the scope of the variable is left, the old value is restored.