← Back to C++ Course | Chapter 16: Modern C++ | Lesson 1 of 9

C++ auto Keyword

auto lets the compiler figure out a variable's type from what you put in, like a label that fills itself in. You write less, but the type is still fixed.
Syntax
cpp
auto variable_name = value;  // type is deduced from value
auto *pointer_name = &variable;
auto &reference_name = variable;

Type Inference Basics

The auto keyword tells the compiler to deduce a variable's type from its initializer, rather than you spelling it out explicitly.

auto x = 5; makes x an int just as if you'd written int x = 5; -- the type isn't dynamic or looser, it's determined once at compile time and fixed from then on.

Example: Type Inference Basics

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare x and set it to 5
	auto x = 5;
	// Print to the console with cout
	std::cout << x << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

auto with Complex Types

auto can be combined with * and & to control exactly what's deduced -- auto* p requires the initializer to be a pointer, and auto& deduces a reference type, which matters when the difference between a copy and a reference changes program behavior.

Example: auto with Complex Types

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare value and set it to 10
	int value = 10;
	auto *p = &value;
	auto &ref = value;
	// Print to the console with cout
	std::cout << *p << " " << ref << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

auto with Loops

Range-based for loops are one of the most common places to see auto, since spelling out a container's full iterator or element type (like std::vector<std::pair<std::string,int>>::iterator) is often long and easy to get wrong -- for (auto& item : container) sidesteps that entirely.

Example: auto with Loops

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>

// Program execution starts in main()
int main() {
	// Create a vector named nums (a resizable array)
	std::vector<int> nums = {1, 2, 3};
	// Range-based for loop: visit every element of nums
	for (auto n : nums) std::cout << n << " ";
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

auto for Return Types

Declaring a function's return type as auto lets the compiler infer it from the return statement inside the function body, useful for template functions where the exact return type would otherwise be hard to write out or depend on the template parameters.

Example: auto for Return Types

cpp
// Include std::cout and std::cin
#include <iostream>

auto add(int a, int b) {
	// Send a + b back to the caller
	return a + b;
}

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << add(2, 3) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

auto with Lambdas

Lambda expressions are usually stored in auto-declared variables since a lambda's actual type is a compiler-generated, unnameable closure type -- you literally cannot write out its type by hand. auto is also used for generic lambda parameters ([](auto x, auto y)), letting one lambda accept multiple argument types.

Example: auto with Lambdas

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Define a lambda (anonymous function) stored in square
	auto square = [](int x) { return x * x; };
	// Print to the console with cout
	std::cout << square(5) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Declaring auto x; with no initializer, which cannot deduce a type.
  2. Expecting auto to keep references or const, when it drops them unless you write auto&.
  3. Overusing auto so the type is unclear to readers.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.