Lesson 3: auto — The Compiler Finds the Type
In C you always wrote the type: int count = 10;. In C++ there's one handy shortcut we'll learn today: auto. When you write auto count = 10;, the compiler looks at the value (10) and infers on its own that the type is int. It's not a 'typeless variable' — the type is fixed at compile time and stays f
auto tells the compiler: 'look at the value and you decide the type'. auto x = 10; makes x an int because 10 is an int.
- auto
- A keyword asking the compiler to infer the type from the initializer. auto x = 10; makes x an int.
- type inference
- The process where the compiler decides the type from the value, without you writing it explicitly.
- initializer
- The value given to a variable when it's declared. With auto it's required — without it there's nothing to infer the type from.