Primitive types
Primitive types are the built-in scalars the compiler already knows: integers, floating-point numbers, bool, and char variants. They occupy a fixed number of bytes and have no constructor or destructor of their own.
Detailed Explanation
You are counting failed login attempts. That count is a whole number that must fit in a register and add in one instruction. C++ gives you integer types for that job, not a class. The same idea applies to a temperature (double), a yes-or-no flag (bool), and a single character (char). These are the primitive types.
They exist because the hardware already has them. A 32-bit int is the width of an arithmetic instruction on most machines you will ship to. Wrapping every number in a class would add construction, padding, and function-call overhead for no gain. The language therefore ships a small set of scalars and lets you build everything else on top.
The integer family is signed or unsigned, and several widths exist. int is the default working size: at least 16 bits, 32 bits on every desktop and server you will meet. short is at least 16 bits. long is at least 32. long long is at least 64. The standard does not pin exact sizes, only minimums and a ranking (short ≤ int ≤ long ≤ long long). When you need an exact width, use the aliases in <cstdint>: std::int32_t, std::uint64_t, std::int_fast32_t.
Unsigned types wrap on overflow using modular arithmetic. Signed overflow is undefined behaviour, which means the compiler may assume it never happens and delete checks you thought were there. That is why production counters that cannot go negative still often use a signed type, or an unsigned type with an explicit wrap policy you document.
Floating-point types are float (usually IEEE-754 binary32), double (binary64), and long double (wider, and not portable across ABIs). They cannot represent every decimal. 0.1 + 0.2 is not exactly 0.3. Use them for measured quantities. Use integers for money, or a decimal library.
bool is its own type, not a disguised int. It converts to 0 or 1 in arithmetic. char is a distinct type from both signed char and unsigned char; whether plain char is signed is implementation-defined. For raw bytes, prefer unsigned char or std::byte (C++17). wchar_t, char16_t, char32_t, and char8_t (C++20) exist for wider encodings; modern text handling uses UTF-8 in std::string plus char8_t when you need the type system to say so.
sizeof on a primitive is a compile-time constant. sizeof(char) is always 1, by definition of "byte" in C++, not because a byte is eight bits. On every machine you will write for, a byte is eight bits.
| Type | Typical size | Notes |
|---|---|---|
bool | 1 byte | Still a full byte; no packed bit unless you use a bit-field or vector<bool> |
char | 1 byte | Encoding and signedness are not portable |
int | 4 bytes | Default arithmetic type |
long | 4 or 8 bytes | 4 on LLP64 Windows, 8 on LP64 Unix |
long long | 8 bytes | At least 64 bits |
float / double | 4 / 8 bytes | IEEE-754 on mainstream platforms |
Value-initialising a primitive (int n{} or int n = 0) sets it to zero. A bare int n; at block scope is uninitialised: reading it is undefined behaviour.
Code Examples
#include <cstdint>
#include <iostream>
#include <limits>
int main() {
int attempts = 0; // signed, default width
std::uint32_t ip = 0xC0A80101; // exact 32-bit IPv4 address
double tempC = 36.6;
bool locked = true;
char grade = 'A';
std::cout << sizeof(attempts) << '\n'; // typically 4
std::cout << std::numeric_limits<int>::max() << '\n';
std::cout << locked << '\n'; // prints: 1
std::cout << static_cast<int>(grade) << '\n'; // prints: 65 on ASCII
(void)ip;
(void)tempC;
}#include <iostream>
int main() {
unsigned wrap = 0;
wrap = wrap - 1u; // well-defined: becomes the max value
std::cout << wrap << '\n';
// int boom = std::numeric_limits<int>::max() + 1; // undefined behaviour
}Common Gotchas
Reading an uninitialised primitive is undefined behaviour
`int n; std::cout << n;` is not "whatever was on the stack". The compiler may constant-fold the read into anything, including deleting a later branch.
`long` is not 64-bit on Windows
LLP64 keeps `long` at 32 bits. Use `long long` or `std::int64_t` when you mean 64 bits.
Unsigned wrap is silent
a loop `for (unsigned i = n; i >= 0; --i)` never ends, because `i >= 0` is always true.
`char` is a distinct type
overloads on `char`, `signed char`, and `unsigned char` are three different functions. Templates that treat "byte" as `char` will surprise you on platforms where `char` is signed.