Auto Keyword in C++

Level 250 keystrokes

Lets the compiler deduce types with auto (x becomes int, y becomes double) and prints their sum, 45.14.

C++ code

// auto takes the type from the initializer: 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;
// int + double is computed as double: 42 + 3.14 prints 45.14
3cout << x + y << endl;

Output

45.14
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.

  • <× 4
  • ;× 3
  • =× 2
  • .× 1
  • +× 1

More C++ examples at level 2

← C++ typing practice