Skip to content

Types and variables

A variable is a name that refers to a place in memory where you store a value. In C++, every variable has a type: the type defines what values it can hold, what operations you can perform, and how much memory it uses.

int age = 20; // integer
double price = 9.99; // decimal

The general declaration form is:

type name = value;

Fundamental types (the ones you’ll use most)

Section titled “Fundamental types (the ones you’ll use most)”
TypeTypical useExample
intGeneral integer (most common)int counter = 0;
shortSmall integershort day = 15;
longLarge integerlong population = 8000000L;
long longVery large integerlong long id = 9000000000LL;

Unsigned versions (unsigned) store only zero or positive values, but can hold larger numbers in the same space.

unsigned int points = 100;
unsigned char byte = 255;

In university exercises, int is usually enough for counters, indexes, and grades.

Floating point: numbers with a fractional part

Section titled “Floating point: numbers with a fractional part”
TypePrecisionTypical use
floatLower (32 bits)Graphics, sensors (when size matters)
doubleHigher (64 bits)Most used for decimal math
long doubleEven higherScientific computing (less common at first)
float temperature = 36.5f; // the 'f' marks it as float
double pi = 3.1415926535;

Recommendation: use double by default for decimals unless float is explicitly required.

char stores a single character (1 byte on most systems).

char letter = 'A';
char symbol = '@';
char digit = '7'; // the character '7', not the number 7

Characters use single quotes ' '. Text strings use double quotes " " (see std::string below).

Useful special values:

char newline = '\n';
char tab = '\t';

bool can only be true or false. Used in conditions, flags, and validation.

bool passed = true;
bool hasError = false;
if (passed) {
// ...
}

In older C++ style, any non-zero number can act as “true”, but always use true/false for clarity.

For full text (multiple characters), use std::string from the standard library:

#include <string>
std::string name = "Ana";
std::string greeting = "Hello, " + name;

char = one character. std::string = full text.

Common forms when starting out:

int a = 10; // initialization with =
int b(20); // direct initialization
int c{30}; // brace initialization (recommended: avoids narrowing)
int d; // uninitialized (may contain garbage; avoid at first)

Good practice: always initialize your variables. Uninitialized variables may hold random “garbage” from memory.

Declare several of the same type:

int x = 1, y = 2, z = 3;

If data must not change, mark it as constant:

const double VAT = 0.16;
const int MAX_ATTEMPTS = 3;
  • const: value cannot change after assignment.
  • constexpr: constant the compiler can evaluate at compile time (useful for fixed sizes and simple formulas).
constexpr int TABLE_SIZE = 10;

By convention, many programmers write constants in UPPERCASE.

auto asks the compiler to deduce the type from the value:

auto age = 20; // compiler deduces int
auto price = 19.99; // deduces double
auto name = std::string("Luis");

Useful when the type is long or obvious, but while learning, prefer writing the type explicitly until you’re comfortable.

Exact sizes depend on the system (32 or 64 bits), but you can check with sizeof:

#include <iostream>
int main() {
std::cout << "int: " << sizeof(int) << " bytes\n";
std::cout << "double: " << sizeof(double) << " bytes\n";
std::cout << "char: " << sizeof(char) << " bytes\n";
std::cout << "bool: " << sizeof(bool) << " bytes\n";
return 0;
}

Typical values on many modern PCs:

TypeApproximate size
char1 byte
bool1 byte
int4 bytes
float4 bytes
double8 bytes
long long8 bytes

Sometimes you need to change a value from one type to another.

The compiler converts in some cases on its own:

int whole = 5;
double decimal = whole; // int -> double (safe)

Warning: double to int loses decimals:

double pi = 3.99;
int truncated = pi; // truncated is 3, not 4
double price = 9.99;
int wholePrice = static_cast<int>(price); // modern, clear form

Prefer static_cast over old-style casts like (int)price.

int a = 10, b = 3;
int sum = a + b; // 13
int remainder = a % b; // 1 (modulo, integers only)
double x = 10.0, y = 3.0;
double quotient = x / y; // 3.333...
bool active = true;
bool off = !active; // false (negation)

Common mistake: 10 / 3 with integers gives 3, not 3.33. For decimals, use at least one double:

double result = 10.0 / 3; // 3.333...
#include <iostream>
#include <string>
int main() {
std::string name;
int age;
std::cout << "What is your name? ";
std::cin >> name;
std::cout << "How old are you? ";
std::cin >> age;
std::cout << "Hello " << name << ", you are " << age << " years old.\n";
return 0;
}

std::cin reads from the console. The variable’s type determines how user input is interpreted.

You need to store…Recommended type
Counter, age, whole gradeint
Money, averages, measurementsdouble
Yes / no, validationbool
One letter or symbolchar
Name, message, textstd::string
Value that never changesconst or constexpr
  • Confusing '7' with 7: the first is char, the second is int.
  • Not initializing variables and then using them.
  • Dividing integers expecting decimals (5 / 2 gives 2).
  • Mixing types unintentionally in expressions (int + double works, but understand the conversion).
  • Using float when double is more precise for academic calculations.

With types clear, continue with functions to organize your code, or review basic syntax if you need to reinforce program structure.