tags:

views:

214

answers:

3

In my college days I read about the auto keyword and in the course of time I actually forgot what it is. It is defined as:

defines a local variable as having a local lifetime

I never found it is being used anywhere, is it really used and if so then where is it used and in which cases?

+4  A: 

auto is a modifier like static. It defines the storage class of a variable. However, since the default for local variables is auto, you don't normally need to manually specify it.

This page lists different storage classes in C.

Mehrdad Afshari
+3  A: 

If you'd read the IAQ (Infrequently Asked Questions) list, you'd know that auto is useful primarily to define or declare a vehicle:

auto my_car;
Jerry Coffin
+1 For enlightening my day! I had forgotten about the IAQ.
Thomas Matthews
+1  A: 

In C auto is a keyword that indicates a variable is local to a block. Since that's the default for block-scoped variables, it's unnecessary and very rarely used (I don't think I've ever seen it use outside of examples in texts that discuss the keyword). I'd be interested if someone could point out a case where the use of auto was required to get a correct parse or behavior.

However, in the upcoming C++ standard 0x the auto keyword will be 'hijacked' to support type inference, where they type of a variable can be taken form the type of whatever is initializing it:

auto someVariable = 5;   // someVariable will have type int

Type inference is being added mainly to support declaring variables in templates or returned from template functions where types based on a template parameter (or deduced by the compiler when a template is instantiated) can often be quite painful to declare manually.

Michael Burr