The auto keyword in C++

Level 3118 keystrokes

auto lets the compiler deduce a variable's type from its initializer: int for 42, double for 3.14 and std::string for string("Alice"). Prints 42, 3.14 and Alice.

C++ code

// An integer literal, so x is deduced as int
1auto x = 42;
// A literal with a decimal point is a double
2auto pi = 3.14;
// string(...) makes it std::string; auto with just "Alice" would give const char*
3auto name = string("Alice");
// The deduced types print as usual: 42, 3.14, Alice
4cout << x << endl;
5cout << pi << endl;
6cout << name << endl;

Output

42
3.14
Alice
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C++ code and how often they appear.

  • <× 12
  • ;× 6
  • =× 3
  • "× 2
  • .× 1
  • (× 1
  • )× 1

More C++ examples at level 3

← C++ typing practice