Search code examples
ckeyword

Concept of "auto" keyword in C


What is the exact concept of the keyword auto in a C program?

When I went through the book Deep C secrets, I saw this quote:

The auto keyword is apparently useless. It is only meaningful to a compiler-writer making an entry in a symbol table. It says this storage is automatically allocated on entering the block (as opposed to global static allocation, or dynamic allocation on the heap). Auto is irrelevant to other programmers, since you get it by default.


Solution

  • auto isn't a datatype. It's a storage class specifier, like static. It's basically the opposite of static when used on local variables and indicates that the variable's lifetime is equal to its scope. When it goes out of scope it is "auto"-matically destroyed.

    Note that auto in C is currently (ie: in C17 and earlier) not like auto in modern C++ -- it does not perform any type inference. However, it is possible that C23 will change auto to do a weaker form of type inference. The auto keyword is pretty useless today: you never need to specify it as the only places you're even allowed to are exactly the places where automatic storage is already the default behavior.