See also: C syntax § Storage class specifiers
(Called automatic variables.)
All variables declared within a block of code are automatic by default. An uninitialized automatic variable has an undefined value until it is assigned a valid value of its type. 5 The storage-class specifier auto 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 class register 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.6
In C++, 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, called Resource Acquisition Is Initialization (RAII).
Since C++11, C++ allows variables to be declared with the auto type specifier,7 but this means that the variable's type is inferred, and does not refer to the scope of the variable.
(Called local variables.)
Similar to C and C++, but there is no auto or register 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.8 This differs from instance variables, which are implicitly initialized with default values (which are 0 for numbers and null for objects).
(Called lexical, my or private variables.)
In Perl, local variables are declared using the my operator. Uninitialized scalars will have the value undef; uninitialized arrays or hashes will be ().9
Perl also has a local operator that does not create automatic variables,10 instead giving global (package) variables a temporary value, which is dynamically scoped to the enclosing block. When the scope of the variable is left, the old value is restored.
unless it is a nested function, which itself is defined along that local data /wiki/Nested_function ↩
although they exist in a somewhat similar, but not identical, form also in recursive languages with dynamic scoping, such as older variants of LISP /wiki/Dynamic_scoping ↩
unless otherwise specified, such as static or heap-based data, which are specifiable in some languages ↩
When the reentrant property of the routine is used, for recursion or otherwise, the optimizer must not try to allocate such variables in processor registers (for efficiency) as this would break the reentrancy. /wiki/Compiler_optimization ↩
Current[update] "C standard" (PDF). (3.61 MiB): section 6.2.4, Storage durations of objects https://en.wikipedia.org/w/index.php?title=Automatic_variable&action=edit ↩
"Storage Duration", cppreference.com https://en.cppreference.com/w/c/language/storage_duration ↩
"Placeholder type specifiers", cppreference.com http://en.cppreference.com/w/cpp/language/auto ↩
"4.12.5 Initial Values of Variables". Sun Microsystems. Retrieved 2008-10-17. http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.12.5 ↩
"Private variables via my() - perlsub - perldoc.perl.org". Retrieved 2008-10-17. http://perldoc.perl.org/perlsub.html#Private-Variables-via-my() ↩
"Temporary values via local() - perlsub - perldoc.perl.org". Retrieved 2011-02-25. http://perldoc.perl.org/perlsub.html#Temporary-Values-via-local%28%29 ↩