C++ Basics: Data Types
Explore fundamental data types, type modifiers, and how C++ represents numbers, characters, and booleans in memory.
Integer Types
Integer types store whole numbers. C++ provides several sizes so you can balance memory usage against the range of values you need.
Integer Types
Use int for most whole-number work; switch to long long when values exceed roughly 2 billion.
- short int: at least 16 bits, range roughly -32,768 to 32,767
- int: at least 16 bits, typically 32 bits on modern systems
- long int: at least 32 bits
- long long int: at least 64 bits (C++11), use for very large numbers
Integer Types
C++Declare and print four integer sizes.
Signed and Unsigned Integers
By default, integer types are signed (they can hold negative values). Adding unsigned removes negative numbers but doubles the positive range.
signed / unsigned
Use unsigned when a value can never be negative (sizes, counts) to catch logic errors at compile time and gain extra range.
- signed int: range -2,147,483,648 to 2,147,483,647 (32-bit)
- unsigned int: range 0 to 4,294,967,295 (32-bit)
- Mixing signed and unsigned in comparisons can cause subtle bugs
- Use size_t (unsigned) for array indices and sizes
signed vs unsigned
C++unsigned int holds larger positive values than signed int.
Floating-Point Types
Floating-point types store real numbers with a fractional part. The three types differ in precision and memory usage.
float, double, long double
Prefer double for most decimal work; float saves memory when precision requirements are low.
- float: 4 bytes, ~6-7 significant decimal digits
- double: 8 bytes, ~15-16 significant decimal digits (default floating-point literal type)
- long double: 8-16 bytes (platform-dependent), highest precision
- Append f to a literal for float: 3.14f; no suffix means double
float vs double Precision
C++See how float loses accuracy compared to double.
Character Types
char stores a single character as its ASCII integer value. For Unicode support, C++ provides wchar_t, char16_t, and char32_t.
char and Wide Characters
char holds one byte (ASCII); use char16_t or char32_t when you need Unicode (UTF-16/UTF-32) characters.
- char: 1 byte, stores ASCII values 0-127 (or 0-255 unsigned)
- wchar_t: 2 or 4 bytes (platform-dependent), wide characters
- char16_t: exactly 2 bytes, UTF-16 (C++11)
- char32_t: exactly 4 bytes, UTF-32 (C++11)
- Character literals use single quotes: 'A'; string literals use double quotes: "A"
char and ASCII
C++A char is just a small integer, you can print it as a character or as its numeric value.
Boolean Type
bool holds exactly two values: true or false. Internally, true is stored as 1 and false as 0.
bool
Use bool for flags and conditions to make code self-documenting instead of using magic integers 0 and 1.
- true evaluates to 1 when used in arithmetic
- false evaluates to 0
- Any non-zero integer converts to true; 0 converts to false
- Use boolalpha manipulator to print true/false instead of 1/0
Boolean Type
C++Print a bool as text and as its numeric equivalent.
void
void represents the absence of a type. It is used as the return type for functions that return nothing, and with pointers to create generic pointers.
void
void tells the compiler and reader that a function performs an action but does not produce a value.
- A void function uses return; (no value) or omits the return statement
- void* is a generic pointer that can point to any type (used in C-style APIs)
- You cannot declare a plain void variable
void Return Type
C++A function that performs output but returns no value.
Type Modifiers Summary
Type modifiers adjust the size or sign of a base type. They can be combined: unsigned long long int is valid.
Modifiers: signed, unsigned, short, long
Modifiers let you fine-tune memory usage and value range without changing the fundamental type.
- signed: default for int and char; allows negative values
- unsigned: only non-negative, doubles positive range
- short: reduces int to at least 16 bits
- long: increases int to at least 32 bits; long long to at least 64 bits
| Type | Typical Size | Range (approx.) |
|---|---|---|
| short int | 2 bytes | -32,768 to 32,767 |
| unsigned short int | 2 bytes | 0 to 65,535 |
| int | 4 bytes | -2.1B to 2.1B |
| unsigned int | 4 bytes | 0 to 4.3B |
| long long int | 8 bytes | -9.2 quintillion to 9.2 quintillion |
| float | 4 bytes | ~6-7 decimal digits |
| double | 8 bytes | ~15-16 decimal digits |
| char | 1 byte | -128 to 127 (signed) |
| bool | 1 byte | true (1) or false (0) |
Knowledge Check
1. Which data type stores true or false?
2. What is the size of int on most 64-bit systems?
3. Which type provides the most decimal precision?
4. What does the unsigned modifier do to an integer type?
5. Which type stores a single character?
6. What is the return type of a function that returns nothing?