C++ needs to know what kind of value each variable holds. Here are the types you will use most:
| Type | What it holds | Example |
|---|---|---|
int | Whole numbers | int year = 2025; |
double | Numbers with a decimal point | double price = 9.99; |
char | One single character, in single quotes | char grade = 'A'; |
bool | true or false | bool isOpen = true; |
string | Text, in double quotes | string name = "Asha"; |
#include <iostream>
#include <string>
using namespace std;
int main() {
int year = 2025;
double price = 9.99;
char grade = 'A';
bool isOpen = true;
string name = "Asha";
cout << "Year: " << year << "\n";
cout << "Price: " << price << "\n";
cout << "Grade: " << grade << "\n";
cout << "Open? " << isOpen << "\n";
cout << "Name: " << name << "\n";
return 0;
}Output
Year: 2025 Price: 9.99 Grade: A Open? 1 Name: Asha
Notice two things:
- To use
string, add#include <string>at the top. boolprints as1for true and0for false.
Tip: Want
true/falseprinted as words? Sendboolalphafirst:cout << boolalpha << isOpen;printstrue.
Single quotes vs double quotes
This trips up a lot of beginners:
'A'– single quotes – is achar: exactly one character."A"– double quotes – is astring: text (which can be one character, many, or none).
Whole numbers vs decimals
An int cannot hold a decimal part. If you put one in, the part after the point is simply cut off:
#include <iostream>
using namespace std;
int main() {
int whole = 7.9; // the .9 is thrown away
double exact = 7.9;
cout << whole << "\n";
cout << exact << "\n";
return 0;
}Output
7 7.9
Bigger and smaller number types
You will sometimes see these too:
| Type | Use it for |
|---|---|
long long | Very big whole numbers (up to about 9 quintillion) |
float | Decimals with less precision than double (uses less memory) |
unsigned int | Whole numbers that are never negative |
#include <iostream>
using namespace std;
int main() {
int small = 2000000000;
long long big = 9000000000000;
cout << small << "\n" << big << "\n";
return 0;
}Output
2000000000 9000000000000
Remember: When in doubt:
intfor whole numbers,doublefor decimals,stringfor text.