Lesson 2 / 10

Variables, Data Types and the Compiler

Like Java, C++ is statically typed — every variable’s type is fixed at compile time and checked by the compiler before your program ever runs.

int age = 25;ndouble price = 19.99;nchar grade = 'A';nbool isActive = true;nstd::string name = "Tutoline";

Common types

  • int — a whole number, typically 32 bits
  • double — a decimal number, the standard choice for most floating-point math
  • char — a single character, written in single quotes
  • bool — true or false
  • std::string — text, from the standard library (note the std:: prefix, covered below)

The std:: prefix

C++’s standard library lives inside a “namespace” called std, which is why you’ll see std::cout, std::string, and std::vector throughout this course. Namespaces prevent naming collisions — your own code could define a class called string without conflicting with the standard library’s std::string, because they live in different namespaces.

Why the compiler matters

The compiler checks your types before the program ever runs — assigning a std::string to an int variable is caught immediately as a compile error, rather than surfacing later as a confusing runtime bug. This upfront checking, combined with C++’s lack of a safety net at runtime for many other kinds of mistakes, is why getting comfortable reading compiler error messages is a genuinely valuable skill in this language.

Constants

const double TAX_RATE = 0.18;n// TAX_RATE = 0.20; -- this line would fail to compile

const tells the compiler a value should never change after it’s initialized — trying to reassign it becomes a compile-time error, not a runtime surprise discovered much later.