Variables and Types
To solve problems, we need to store and work with data like numbers, letters, or words. We store data in variables.
Example
#include <iostream>
using namespace std;
int main() {
int age = 15;
double height = 1.70;
char grade = 'A';
string name = "Ali";
cout << name << " is " << age << " years old.";
return 0;
}Output:
Ali is 15 years old.
Explanation
int→ whole numbers (like3,-7,2025)double→ numbers with decimals (like3.14or-0.5)char→ a single letter or symbol (like'A','z','?')string→ text or sequence of characters (like"Hello")
More number types
long long
When numbers get too big for an int (for example, when you count up to billions or trillions), we use long long.
long long population = 8000000000; // 8 billionWhy? Because an int can usually store values up to about 2 billion (2,147,483,647). If we need larger numbers, we must use long long.
float and double
Both store *decimal numbers, but there is a difference in precision** (how many digits they can store correctly):
| Type | Precision | Example |
|---|---|---|
float |
about 6–7 digits | 3.141593 |
double |
about 15–16 digits | 3.141592653589793 |
float f = 1.61;
double d = 1.61;
cout << setprecision(20); // show 20 decimal places
cout << f << endl; // prints 1.6100000143051147461
cout << d << endl; // prints 1.6100000000000000977As you can see in the example, most real numbers cannot be stored exactly using floats or doubles. This is something you need to keep in mind whenever you work with these types.
Most of the time, programmers use double because it is more precise. However, double uses 8 bytes of memory, while float uses only 4. Use float only if you really need to save memory.
bool
A bool variable can take only two different values: true or false.
bool isEven = true;
bool isPrime = false;You’ll often use it for conditions and logic in if statements:
int x = 7;
bool big = x > 10;
cout << big; // prints 0 (false)true is represented internally as 1, and false as 0.
Example: Input and Output with Variables
int x, y;
cin >> x >> y;
cout << x + y;Input:
4 9
Output:
13
That’s how many simple problems on platforms like Codeforces or AtCoder look!
Type summary table
| Type | Meaning | Example | Memory (approx.) |
|---|---|---|---|
int |
whole number | 5, -10 |
4 bytes |
long long |
large whole number | 1000000000000 |
8 bytes |
float |
decimal (low precision) | 3.14 |
4 bytes |
double |
decimal (high precision) | 3.1415926535 |
8 bytes |
bool |
true/false | true |
1 byte |
char |
one character | 'A' |
1 byte |
string |
text | "Hello" |
depends on length |
Type conversion
Sometimes we mix types:
int a = 3;
double b = 2.5;
cout << a + b; // 5.5C++ automatically converts a to a double temporarily, so the sum is also a double.
Optional: auto
auto lets the computer guess the type for you:
auto x = 5; // int
auto y = 3.14; // double
auto word = "Hi"; // const char*You’ll mostly use it in more advanced problems later.
Naming Variables
When you create variables in C++, you must choose a name for each one. This name tells the program (and humans!) what the variable represents.
Good names make your code easier to read and understand.
Rules for naming variables
Here are the basic rules you must follow:
You can: - Use letters, digits, and underscores (_) - Start with a letter or an underscore - Use both uppercase and lowercase letters (C++ is case-sensitive)
You cannot: - Start the name with a number - Use spaces - Use special symbols like @, $, %, !, #, ?, etc. - Use C++ keywords (like int, for, return, while, etc.)
Examples
Correct names:
int age;
double total_price;
bool isValid;
string firstName;Incorrect names:
int 2cats; // starts with a number
int my variable; // spaces not allowed
int total$; // special character
int int; // keywordCase sensitivity
C++ treats uppercase and lowercase letters as different characters:
int score = 10;
int Score = 20;
cout << score; // prints 10Here, score and Score are two different variables.
Naming style tips
There are different naming styles, but most C++ programmers use one of these:
| Style | Example | Common use |
|---|---|---|
| snake_case | player_health |
variables and functions in simple programs |
| camelCase | playerHealth |
used often in larger projects or libraries |
| PascalCase | PlayerHealth |
used for classes or structs |
Pick one style and be consistent throughout your code.
Example
#include <iostream>
using namespace std;
int main() {
int playerHealth = 100;
int playerScore = 2500;
bool isAlive = true;
cout << "Health: " << playerHealth << endl;
cout << "Score: " << playerScore << endl;
cout << "Alive: " << isAlive << endl;
return 0;
}Output:
Health: 100
Score: 2500
Alive: 1
Quick summary
| Rule | Allowed | Example |
|---|---|---|
| Start with letter or underscore | YES | _count, sum |
| Start with number | NO | 9lives |
| Use space | NO | total price |
| Use underscore | YES | total_price |
| Use symbol ($, #, @, etc.) | NO | total$ |
| Use C++ keyword | NO | int, while |
Good variable names make your code cleaner and easier to debug!