C++ Fundamentals

Lesson 02 · 15 min

Types, Variables & Operators

int vs long long, bool, char, overflow traps, and operator basics.

Types that matter for DSA

Integers

TypeTypical range (64-bit)Use when
int~ ±2×10⁹Default indices, small counts
long long~ ±9×10¹⁸Sums, products, big constraints
size_tunsignedContainer sizes (careful with underflow)

Rule: if constraints say n ≤ 1e5 and you sum a[i] up to 1e9, sum can exceed int → use long long.

Overflow classic

int a = 1e9, b = 1e9;
long long bad = a * b;           // WRONG: multiply happens in int first
long long good = 1LL * a * b;    // OK

bool & comparisons

true/false. Prefer == for equality, never = inside if.

const & references (preview)

const vector<int>& nums means: borrow the vector, don't copy, don't modify.

Rules to remember

  • Prefer long long for sums/products under large constraints.
  • Multiply with 1LL * a * b to avoid int overflow.
  • int is fine for indices 0..n-1 when n ≤ 1e7-ish.
  • Never assign inside if: write if (x == y), not if (x = y).

Check yourself

1. a and b are int = 1e9. Safest product?

Practice what you learned