C++ Basics · Chapter 2

Variables, Data Types & Expressions in C++

Learn how to store, manipulate, and display data in C++ — the foundation of every program you will ever write.

By Arj Updated July 2025 12 min read No prior experience needed
What you'll learn
  • What variables are and why they are essential
  • The built‑in data types: int, double, char, bool, and string
  • How to declare, initialise, and use variables correctly
  • Arithmetic operators and expression evaluation
  • Type conversion (implicit and explicit) and constants
  • How to read user input with cin

1What is a variable?

A variable is a named container in your program that holds a value. Think of it as a labelled box: you put something in, you can change it later, and you can refer to it by name whenever you need its current value.

In C++, every variable has a fixed data type that tells the compiler what kind of data it can store (e.g., integers, decimals, characters). This strict typing helps catch errors early and makes the program more efficient.

Here is a simple declaration and initialisation:

cpp
int age = 18;
double temperature = 36.5;
char grade = 'A';
bool passed = true;

The variable name (e.g., age) is used to refer to the stored value. You can change it later by assigning a new value of the same type.

Why variables matter
Without variables, you could not store data, compute results, or make decisions based on changing input. Every non‑trivial program relies on them.

2Built‑in data types in C++

C++ provides several fundamental data types. Each type defines the range of values it can hold and the operations you can perform.

Data TypeDescriptionExample
intWhole numbers (no decimal part)int count = 10;
doubleFloating‑point numbers (decimals)double pi = 3.14159;
charA single character (enclosed in single quotes)char grade = 'B';
boolBoolean (true or false)bool flag = false;
stringText (needs #include <string>)string name = "Ali";
Remember the string header
To use string, you must include <string> at the top of your file. Without it, the compiler will not recognise the type.

3Declaration and initialisation

Declaration tells the compiler about the variable's name and type. Initialisation gives it an initial value.

You can declare and initialise in one line, or separately:

cpp
int age;          // declaration only
age = 18;        // later initialisation

double score = 9.5; // declaration + initialisation

// Multiple variables of the same type:
int a = 1, b = 2, c = 3;
Uninitialised variables are dangerous
If you declare a variable without giving it a value, it holds a garbage value (whatever happened to be in that memory location). Using it can cause unpredictable behaviour. Always initialise variables before you use them.

4Using cout with different data types

You can print variables of any type using std::cout (or cout if you have using namespace std;). The output operator << streams the values to the console.

cpp
#include <iostream>
#include <string>
using namespace std;

int main() {
    int year = 2025;
    double gpa = 3.9;
    string name = "Arj";
    char grade = 'A';

    cout << "Name: " << name << endl;
    cout << "Year: " << year << endl;
    cout << "GPA: " << gpa << endl;
    cout << "Grade: " << grade << endl;

    return 0;
}
OutputName: Arj Year: 2025 GPA: 3.9 Grade: A

5Expressions and arithmetic operators

An expression is a combination of values, variables, and operators that produces a new value. C++ provides the usual arithmetic operators.

OperatorMeaningExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulo (remainder)a % b
cpp
int a = 10, b = 3;
cout << (a + b) << endl; // 13
cout << (a - b) << endl; // 7
cout << (a * b) << endl; // 30
cout << (a / b) << endl; // 3 (integer division)
cout << (a % b) << endl; // 1
Output13 7 30 3 1
Watch out for integer division
When both operands of / are integers, the result is truncated (the fractional part is discarded). To get a decimal result, make at least one operand a floating‑point number, e.g., 10.0 / 3.

6Type conversion

Implicit conversion happens automatically when you assign a value of one type to a variable of another compatible type. For example, assigning an int to a double is safe because the integer can be represented as a decimal.

cpp
int x = 5;
double y = x; // implicit conversion: int → double
cout << y << endl; // prints 5.0

Explicit conversion (casting) is when you force the conversion. This is useful to avoid integer division.

cpp
int a = 7, b = 2;
double result = (double) a / b; // cast a to double before division
cout << result << endl; // 3.5
Prefer explicit casting for clarity
Using a cast makes your intention clear to other readers (and your future self) that you are deliberately converting between types.

7Constants

Sometimes you need a value that never changes, like the mathematical constant π. Use the const keyword to declare a variable that cannot be modified after initialisation.

cpp
const double PI = 3.14159;
// PI = 3.14; // error – PI is read‑only

Constants make your code more maintainable: if you need to change the value, you only have to update it in one place.

8Reading input with cin

To get data from the user, use std::cin (or cin). The extraction operator >> reads input and stores it in a variable.

cpp
#include <iostream>
using namespace std;

int main() {
    int age;
    cout << "Enter your age: ";
    cin >> age;
    cout << "You entered: " << age << endl;
    return 0;
}
Sample runEnter your age: 25 You entered: 25
Multiple inputs in one line
You can chain cin to read multiple values: cin >> a >> b; waits for the user to enter two values separated by spaces or newlines.
Try it yourself

Write a program that:

  • Prompts the user for two integers
  • Prints their sum, product, and remainder (using modulo)

Sample output:

ExampleEnter two numbers: 7 3 Sum: 10 Product: 21 Remainder: 1

Hint: use cin >> a >> b; to read both numbers in one line.

9Quiz yourself

Variables & Data Types Quiz
Question 1 of 10
Coming up next
Conditionals and Control Flow
Learn how to make decisions in your code using if, else if, and else statements.
Continue to Chapter 3

Stop wrestling with confusion.

Join thousands of students mastering Computer Science without the academic jargon.

From syntax to systems. We break down the hardest ideas in computer science so you can actually build things.

© 2026 Painless Programming. Built for students.
Scroll to Top