Fundamental data types
4.1 — Introduction to Fundamental Data Types¶
C++ comes with built-in support for several types of data, often called built-in types or fundamental types.
-
Integers: Whole numbers (e.g.,
int). -
Floating-point: Real numbers with fractional parts (e.g.,
double). -
Characters: Single letters or symbols (e.g.,
char). -
Boolean: True/false values (e.g.,
bool). -
Void: The "no type" type.
4.2 — Void¶
void is a special type that represents the absence of a value.
-
You cannot create a variable of type
void(e.g.,void x;will cause an error). -
It is most commonly used as a return type for functions that perform an action but return nothing to the caller.
4.3 — Object Sizes and the sizeof Operator¶
Data types don't have a fixed size in the C++ standard; their size depends on the compiler and architecture (e.g., 32-bit vs 64-bit).
-
sizeofoperator: A unary operator that tells you how many bytes of memory a specific type or variable consumes. -
Example:
sizeof(int)might return 4 on most modern systems.
4.4 — Signed Integers¶
Integers can be signed (hold both positive and negative numbers) or unsigned (positive only).
-
By default,
intis signed. -
Range: A signed \(n\)-bit integer has a range of \(-(2^{n-1})\) to \(2^{n-1}-1\).
-
Integer Overflow: If you try to store a number outside this range, the result is undefined behavior.
4.5 — Unsigned Integers (and Why to Avoid Them)¶
Unsigned integers use the unsigned keyword and can only store non-negative values (\(0\) to \(2^n - 1\)).
-
The Trap: When an unsigned integer goes below zero, it "wraps around" to the largest possible value for that type.
-
Best Practice: Use signed integers for almost everything, even if the value will never be negative. Use unsigned only for bit manipulation or when required by a specific library.
4.6 — Fixed-Width Integers and size_t¶
Because int can be different sizes on different systems, C++ provides types with guaranteed sizes in the <cstdint> header.
-
Examples:
std::int16_t(2 bytes),std::int32_t(4 bytes). -
std::size_t: A special unsigned type used to represent the size or length of objects. It is guaranteed to be large enough to hold the size of the largest object your system can create.
4.7 & 4.8 — Floating Point Numbers¶
Floating point types represent real numbers (e.g., 3.14159).
-
Types:
float(single precision),double(double precision), andlong double. -
Scientific Notation: C++ uses
eto represent powers of 10.1.2e3is \(1.2 \times 10^3 = 1200\). -
Precision Issues: Floating point numbers are approximations. Comparisons like
if (0.1 + 0.2 == 0.3)will often fail due to tiny rounding errors.
4.9 — Boolean Values¶
The bool type holds one of two values: true or false.
-
When converted to integers,
truebecomes1andfalsebecomes0. -
std::boolalpha: A manipulator used withstd::coutto print "true" or "false" instead of "1" or "0".
4.10 — Introduction to if Statements¶
The if statement allows for conditional execution.
-
Syntax:
if (condition) { /* code */ } -
If the condition evaluates to
true(any non-zero value), the code block executes. -
Else: An optional
elseblock executes if the condition isfalse.
4.11 — Chars¶
The char type stores a single character.
-
Characters are actually stored as small integers (usually based on the ASCII table).
-
Literals: Always use single quotes for chars (
'a') and double quotes for strings ("a"). -
Example:
'a'is stored as the value97.
4.12 — Type Conversion and static_cast¶
Sometimes you need to convert a value from one type to another.
-
Implicit Conversion: The compiler does it automatically (e.g., assigning an
intto adouble). -
Explicit Conversion (
static_cast): The programmer manually converts the type.-
Syntax:
static_cast<new_type>(expression) -
Use case: Converting a
charto anintto see its ASCII value, or preventing data loss warnings when converting adoubleto anint.
-