Skip to content
AgileLogics
3 min read

Variables and the auto Keyword

Prerequisites: How C++ differs from C

C++ has the same primitive types as C, plus bool and std::string. The auto keyword lets the compiler infer a variable's type from its initializer — the type is still fixed at compile time, exactly as if you'd written it yourself.

int age = 36;
auto price = 19.99;      // inferred as double
std::string name = "Ada"; // a real, growable string type
bool isActive = true;

Common mistakes

  • Overusing auto where the explicit type would make code clearer to a reader — reach for it mainly when the type is already obvious from the right-hand side, or long/awkward to spell out.