In addition to visibility, every variable also has a certain “lifetime”—that is, the period during the program's execution when the variable exists.
External variables exist for the life of the program. Memory is allocated for them when the program begins and remains until the program ends.
Summary: An automatic variable disappears when the function ends.
Local variables have shorter lifetimes. They come into being when the function begins and disappear when the function ends. For this reason, a local variable is said to be “automatic.” The variable comes and goes automatically, each time the function is called.
Automatic variables conserve memory in a couple of ways. First, since they evaporate when the function ends, automatic variables don't consume memory when not in use. Second, they are stored in the “stack” memory area, which the program allocates at run time. So, automatic variables don't enlarge the executable program.
The C language provides the auto keyword for declaring automatic variables. However, this keyword is seldom used, since all local variables are automatic unless you specify otherwise. In the following function, both val and example are automatic variables:
void sample( void )
{
int val;
auto int example;
}
The auto preceding the declaration of example has no practical effect.
The variable example is automatic even if you remove the auto from its
declaration.