C++ auto Keyword
Example
Let the compiler work out the type of each variable:
auto myNum = 5; // int
auto myDoubleNum = 9.98; // double
auto myLetter = 'D'; // char
cout << myNum << "\n";
cout << myDoubleNum << "\n";
cout << myLetter;
Definition and Usage
The auto keyword lets the compiler work out the type of a variable from the value you assign to it. For example, auto myNum = 5; creates an int, because 5 is an int.
A variable that is declared with auto must get a value at the same time, so auto x; is an error. Once the type is chosen, it does not change.
auto is most useful when the type name is long, like for iterators.
Related Pages
Read more about the auto keyword in our C++ auto Tutorial.