Types and variables
Main idea
Section titled “Main idea”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; // integerdouble price = 9.99; // decimalThe 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)”Integers: numbers without decimals
Section titled “Integers: numbers without decimals”| Type | Typical use | Example |
|---|---|---|
int | General integer (most common) | int counter = 0; |
short | Small integer | short day = 15; |
long | Large integer | long population = 8000000L; |
long long | Very large integer | long 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”| Type | Precision | Typical use |
|---|---|---|
float | Lower (32 bits) | Graphics, sensors (when size matters) |
double | Higher (64 bits) | Most used for decimal math |
long double | Even higher | Scientific computing (less common at first) |
float temperature = 36.5f; // the 'f' marks it as floatdouble pi = 3.1415926535;Recommendation: use double by default for decimals unless float is explicitly required.
Characters: one letter or symbol
Section titled “Characters: one letter or symbol”char stores a single character (1 byte on most systems).
char letter = 'A';char symbol = '@';char digit = '7'; // the character '7', not the number 7Characters use single quotes ' '. Text strings use double quotes " " (see std::string below).
Useful special values:
char newline = '\n';char tab = '\t';Booleans: true or false
Section titled “Booleans: true or false”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.
Text: std::string
Section titled “Text: std::string”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.
How to declare and initialize variables
Section titled “How to declare and initialize variables”Common forms when starting out:
int a = 10; // initialization with =int b(20); // direct initializationint 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;Constants: values that don’t change
Section titled “Constants: values that don’t change”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: let the compiler infer the type
Section titled “auto: let the compiler infer the type”auto asks the compiler to deduce the type from the value:
auto age = 20; // compiler deduces intauto price = 19.99; // deduces doubleauto name = std::string("Luis");Useful when the type is long or obvious, but while learning, prefer writing the type explicitly until you’re comfortable.
How big is each type?
Section titled “How big is each type?”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:
| Type | Approximate size |
|---|---|
char | 1 byte |
bool | 1 byte |
int | 4 bytes |
float | 4 bytes |
double | 8 bytes |
long long | 8 bytes |
Type conversions
Section titled “Type conversions”Sometimes you need to change a value from one type to another.
Implicit conversion (automatic)
Section titled “Implicit conversion (automatic)”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 4Explicit conversion (you request it)
Section titled “Explicit conversion (you request it)”double price = 9.99;int wholePrice = static_cast<int>(price); // modern, clear formPrefer static_cast over old-style casts like (int)price.
Basic operations by type
Section titled “Basic operations by type”int a = 10, b = 3;int sum = a + b; // 13int 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...Input and output with variables
Section titled “Input and output with variables”#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.
Quick summary: which type to use
Section titled “Quick summary: which type to use”| You need to store… | Recommended type |
|---|---|
| Counter, age, whole grade | int |
| Money, averages, measurements | double |
| Yes / no, validation | bool |
| One letter or symbol | char |
| Name, message, text | std::string |
| Value that never changes | const or constexpr |
Common beginner mistakes
Section titled “Common beginner mistakes”- Confusing
'7'with7: the first ischar, the second isint. - Not initializing variables and then using them.
- Dividing integers expecting decimals (
5 / 2gives2). - Mixing types unintentionally in expressions (
int + doubleworks, but understand the conversion). - Using
floatwhendoubleis more precise for academic calculations.
Next step
Section titled “Next step”With types clear, continue with functions to organize your code, or review basic syntax if you need to reinforce program structure.