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.
- 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:
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.
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 Type | Description | Example |
|---|---|---|
int | Whole numbers (no decimal part) | int count = 10; |
double | Floating‑point numbers (decimals) | double pi = 3.14159; |
char | A single character (enclosed in single quotes) | char grade = 'B'; |
bool | Boolean (true or false) | bool flag = false; |
string | Text (needs #include <string>) | string name = "Ali"; |
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:
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;
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.
#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; }
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.
| Operator | Meaning | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulo (remainder) | a % b |
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
/ 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.
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.
int a = 7, b = 2; double result = (double) a / b; // cast a to double before division cout << result << endl; // 3.5
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.
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.
#include <iostream> using namespace std; int main() { int age; cout << "Enter your age: "; cin >> age; cout << "You entered: " << age << endl; return 0; }
cin to read multiple values: cin >> a >> b; waits for the user to enter two values separated by spaces or newlines.
Write a program that:
- Prompts the user for two integers
- Prints their sum, product, and remainder (using modulo)
Sample output:
Hint: use cin >> a >> b; to read both numbers in one line.
9Quiz yourself
if, else if, and else statements.