auto

The auto storage class specifier lets you define a variable with automatic storage; its use and storage is restricted to the current block. The storage class keyword auto is optional in a data declaration. It is not permitted in a parameter declaration. A variable having the auto storage class specifier must be declared within a block. It cannot be used for file scope declarations.

Because automatic variables require storage only while they are actually being used, defining variables with the auto storage class can decrease the amount of memory required to run a program. However, having many large automatic objects may cause you to run out of stack space.

Declaring variables with the auto storage class can also make code easier to maintain, because a change to an auto variable in one function never affects another function (unless it is passed as an argument).

The following example lines declare variables having the auto storage class specifier:

auto int counter;
auto char letter = 'k';

Initialization
You can initialize any auto variable except parameters. If you do not initialize an automatic object, its value is indeterminate. If you provide an initial value, the expression representing the initial value can be any valid C expression. For structure and union members, the initial value must be a valid constant expression if an initializer list is used. The object is then set to that initial value each time the program block that contains the object's definition is entered.

Note: If you use the goto statement to jump into the middle of a block, automatic variables within that block are not initialized.

Storage
Objects with the auto storage class specifier have automatic storage duration. Each time a block is entered, storage for auto objects defined in that block is made available. When the block is exited, the objects are no longer available for use.

If an auto object is defined within a function that is recursively invoked, memory is allocated for the object at each invocation of the block.



Block Scope Data Declaration


goto