Beginner⏱️ 9 min📘 Topic 4 of 32

📦 Variables and Data Types in C++ — int, float, char, bool, string

Master C++ variables and primitive data types — int, float, double, char, bool and string. Learn size, range, type conversion and the gotchas with examples.

A variable is a named storage location for a value. Before you use one, you must declare its type — C++ is strictly typed.

🔢 Primitive types

  • int — whole numbers (typically 4 bytes, ±2.1 billion)
  • long long — bigger whole numbers (8 bytes)
  • float — decimal numbers (4 bytes, ~7 digits precision)
  • double — bigger decimals (8 bytes, ~15 digits precision) — preferred
  • char — a single character: 'A'
  • booltrue or false
  • std::string — text, from <string> header

🧠 Declaration vs initialization

int age;          // declared, uninitialized — garbage value!
int age = 25;     // declared + initialized
int age{25};      // modern brace initialization, safer

🔁 const and auto

  • const — value cannot change: const double PI = 3.14159;
  • auto — let the compiler infer the type from the right side: auto x = 42; // int

⚠️ Type conversion

int / int truncates! 5 / 2 = 2, not 2.5. Cast one side to double: 5.0 / 2.

💻 Code Examples

Declare and use variables

#include <iostream>
#include <string>
int main() {
  int age = 25;
  double price = 9.99;
  char grade = 'A';
  bool isStudent = true;
  std::string name = "Sam";
  std::cout << name << " is " << age;
  return 0;
}
Output:
Sam is 25

Integer division gotcha

int a = 5, b = 2;
std::cout << a / b << '\n';      // 2 (truncated)
std::cout << (double)a / b;       // 2.5 (cast fixes it)
Output:
2
2.5

⚠️ Common Mistakes

  • Using an uninitialized variable — its value is whatever was in memory (garbage).
  • Storing big numbers in `int` and overflowing — use `long long` for anything beyond ~2 billion.
  • Expecting `5/2` to give `2.5` — integer division truncates.
  • Comparing floats with `==` — use a tolerance: `fabs(a - b) < 1e-9`.

🎯 Interview Questions

Real questions asked at top product and service-based companies.

Q1.Why prefer double over float in C++?Beginner
double has roughly twice the precision (15 vs 7 digits) and on modern CPUs is rarely slower. Use float only when memory is very constrained (graphics buffers, embedded).
Q2.What is the difference between `int x;` and `int x = 0;`?Beginner
The first declares x but does NOT initialize it — its value is undefined (could be anything). The second initializes it to 0. Modern style: always initialize.
Q3.What does the `auto` keyword do?Intermediate
It tells the compiler to deduce the variable's type from its initializer. `auto x = 3.14;` makes x a double. Useful for long types like iterators.
Q4.What's the difference between `const int x` and `int const x`?Intermediate
They mean the same thing — both make x a constant int. The position only matters with pointers: `const int*` vs `int* const` differ in what's protected.
Q5.Why is comparing floats with == unreliable?Advanced
Floats can't represent all decimal numbers exactly. `0.1 + 0.2 != 0.3` in floating-point. Compare with a tolerance: `std::abs(a - b) < 1e-9`.

🧠 Quick Summary

  • C++ is strictly typed — declare types before using.
  • Prefer `double` over `float`, `int` for whole numbers.
  • Always initialize — uninitialized variables hold garbage.
  • `auto` infers type from initializer.
  • `const` makes a variable read-only.
  • Integer division truncates; cast for decimal results.