auto keyword in C++

Level 4168 keystrokes

Declares variables with auto so the compiler deduces each type from its initializer, then prints them.

C++ code

// 42 is an int literal, so x is int
1auto x = 42;
// 3.14 is a double literal, so y is double
2auto y = 3.14;
// s is std::string (a plain "Hello" would make it const char*)
3auto s = string("Hello");
// v is vector<int> with three elements
4auto v = vector<int>{1, 2, 3};
5
// Print 42, 3.14 and Hello, one per line
6cout << x << endl;
7cout << y << endl;
8cout << s << endl;
// Print the number of elements in v: 3
9cout << v.size() << endl;

Output

42
3.14
Hello
3
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.

  • <× 17
  • ;× 8
  • =× 4
  • ,× 2
  • .× 2
  • "× 2
  • (× 2
  • )× 2
  • {× 1
  • }× 1

More C++ examples at level 4

← C++ typing practice