Loading, please wait...

Local Variables

Only variables can have automatic storage duration. A function’s local variables (those declared in the parameter list or function body) normally have automatic storage duration.

Keyword auto explicitly declares variables of automatic storage duration. For example, the following declaration indicates that double variables x and y are automatic local variables and they exist only in the body of the function in which the declaration appears:

auto double x, y;

Local variables have automatic storage duration by default, so keyword auto is rarely used. For the remainder of the text, we’ll refer to variables with automatic storage duration simply as automatic variables.

Automatic storage is a means of conserving memory because automatic variables exist only when they’re needed. They’re created when a function is entered and destroyed when the function is exited.

Automatic storage is an example of the principle of least privilege allowing access to data only when it’s absolutely needed. Why have variables stored in memory and accessible when in fact they’re not needed?