Last 30 Days
No notifications
Variables in C++ work like C, plus three superpowers: auto (let the compiler figure out the type), references (&, a safer pointer), and uniform { } initialisation.
int age = 19;
double price = 99.99;
char grade = 'A';
bool passed = true;
string name = "Asha"; // needs <string>auto x = 42; // compiler deduces int
auto pi = 3.14; // double
auto greet = "hello"; // const char*
const int MAX = 100; // never changes after init
int score = 50;
int& ref = score; // ref IS another name for score
ref = 100;
cout << score; // 100 — same variable!References are like pointers, but:
. syntax (not -> or *)int a{42}; // brace init (C++11+)
int b = 42; // also fine
double c{3.14};
int d{}; // d == 0 — zero-initBrace init catches narrowing conversions: int x{3.14}; is a compile error (good!).
Modern C++ keeps the speed of C variables but adds safety, brevity, and a much larger built-in toolkit.
Same as C, with one important addition: bool.
| Type | Size (typical) | Range |
bool | 1 byte | true / false |
char | 1 byte | -128..127 (or 0..255) |
int | 4 bytes | ±2 billion |
long long | 8 bytes | ±9 quintillion |
float | 4 bytes | ~7 digits precision |
double | 8 bytes | ~15 digits precision |
Use int for normal numbers, long long when ints might overflow (CP problems!), double for decimals.
auto x = 42; // int
auto y = 3.14; // double
auto name = string("Asha"); // string
auto v = vector<int>{1,2,3}; // vector<int>Use auto when:
auto when:
// Verbose without auto:
map<string, vector<int>>::iterator it = m.begin();// Clean with auto:
auto it = m.begin();
const int MAX_USERS = 1000; // can never change
const string GREETING = "hello";void print(const string& s); // function won't modify s
const everywhere you can. It catches mistakes at compile time and helps optimisation.
> const vs #define: prefer const (or constexpr). It has a type, respects scope, and shows up in the debugger.
constexpr int FACTORIAL_5 = 5 * 4 * 3 * 2 * 1; // computed at compile time
constexpr double PI = 3.14159265358979;constexpr is "even more const" — guaranteed to be known at compile time. Use it for true constants and array sizes.
The modern, uniform way to initialise:
int a{42};
double b{3.14};
string c{"hello"};
vector<int> v{1, 2, 3, 4};
int zero{}; // zero-initialised — value is 0It also forbids dangerous narrowing conversions:
int x = 3.14; // compiles — silently truncates to 3
int y{3.14}; // ❌ compile error — narrowingPrefer { } for initialisation in modern C++.
A reference is just another name for an existing variable.
int score = 50;
int& ref = score; // ref refers to score
ref = 80;
cout << score; // 80Once bound, a reference can't change which variable it refers to. There's no "null reference". Use them for:
void grow(vector<int>& v) { // & = pass by reference
v.push_back(99); // modifies caller's vector
}void print(const string& s) { // big string — no copy, no modify
cout << s;
}This is the default way to pass anything bigger than a few ints in modern C++.
vector<int> nums = {1, 2, 3};
for (int& n : nums) n *= 2; // modifies in place
for (const int& n : nums) cout << n; // read-only, no copyReference (int&) | Pointer (int*) | ||||
| Can be NULL? | No | Yes | |||
| Reassignable? | No | Yes | |||
| Syntax | normal x.field | p->field | |||
| Use for | function args, range-for | data structures, optional refs | > In modern C++, prefer references unless you genuinely need null or reassignment. They eliminate a whole class of bugs. Type Casting | Need | Use |
| Numeric conversion | static_cast | ||||
Drop const | const_cast (rarely!) | ||||
| Run-time polymorphic check | dynamic_cast (with virtual classes) | ||||
| Reinterpret bits | reinterpret_cast (almost never) |
double d = 3.14;
int n = static_cast<int>(d); // n = 3static_cast covers 99% of conversions safely. Avoid C-style (int)d in new code.
Variables declared inside { } exist only within that block:
int x = 1;
{
int y = 2; // y is alive here
cout << x + y;
}
// y no longer existsInitialise where you declare. Don't put declarations at the top of main() (old K&R style); declare at point of first use.
| Action | Modern C++ |
| Type-deduced var | auto x = 42; |
| True constant | constexpr int N = 100; |
| Read-only var | const int n = 5; |
| Reference | int& r = x; |
| Const reference (param) | void f(const T& x); |
| Brace init | int x{42}; |
| Cast | static_cast |
These five tools — auto, const, constexpr, references, brace init — are the foundation of every modern C++ program.