C Language | Structure Declarations | Automatic Variables - auto

The auto keyword explicitly declares a variable as an automatic variable valid only within the block where it is declared. Variables declared inside blocks are automatic by default, so using auto has little practical meaning in modern C.

Variables with Local Lifetimes

C variable declarations can use storage-class specifiers to define variable lifetimes explicitly. typedef is technically a storage-class specifier, although it is unusual. Ordinary storage classes determine when variables are released from memory.

Storage-Class Specifier

storage-class-specifier type variable ...

You cannot combine multiple storage-class specifiers in one declaration.

A variable declared inside a block, such as a function body, is an automatic variable. It is valid only within that block. It is initialized when control enters the block and released when control leaves it. The compiler automatically treats variables declared inside blocks as automatic variables.

You can explicitly declare an automatic variable with auto. Because this provides no practical advantage, it is generally omitted.

auto Storage-Class Specifier

auto type variableName ...

The meaning of auto differs between C and C++. In C++11, auto indicates type inference. Compiling C code that uses the C meaning of auto as C++ may therefore cause an error.

Code 1

#include <stdio.h>

int main() {
 auto char *str = "Kitty on your lap";
 printf("%s\n" , str);
  return 0;
}

The str variable in Code 1 explicitly uses the auto storage-class specifier. Omitting auto makes no difference. Variables declared inside functions are automatic by default.