Variables and types

What a variable and a type are; int, double, string, bool.

1. What is this?

A variable is a named slot in memory. A type fixes what fits: int (whole numbers), double (decimals), std::string (text, needs #include <string>), bool (true/false). int age = 28 reserves an integer slot called age with value 28.

2. Why do we use it?

C++ gives you control over memory and speed. Types are the contract that makes that control safe.

3. Syntax breakdown

  • int age = 28;

    Integer variable age holding 28.

  • std::string name = "Ava";

    Text variable. Needs #include <string>.

  • std::cout << name << " is " << age << std::endl;

    << chains values into one line of output.

5. Common mistakes

  • Using std::string without #include <string>.
  • Assigning "28" (text) to an int.
  • Single vs double quotes: "Ava" is a string, 'A' is a single char.

Practice task

Add std::string city = "Berlin"; and print "I live in Berlin."

Hint

Declare city, then cout << "I live in " << city << "." << endl;

Try it below — tests run automatically when you press Run.

cpp

Output

Press Run to see output.

Mini quiz

3 questions. Answer, then check.

  1. 1. Which type holds whole numbers?

  2. 2. Which header does std::string need?

  3. 3. What does << do with cout?

4. Example — edit and run

cpp

Output

Press Run to see output.

Expected output: Ava is 28