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.
Best Answer
auto
isn't a datatype. It's a storage class specifier, likestatic
. It's basically the opposite ofstatic
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 likeauto
in modern C++ -- it does not perform any type inference. However, it is possible that C23 will changeauto
to do a weaker form of type inference. Theauto
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.