Data Types
Integers
There are several integer types you can use in C++, but we discuss these 3 for now:
int
- Memory: 4 bytes
- Range: $-2^{31}$ to $2^{31} - 1$ (roughly $-2 \times 10^9$ to $2 \times 10^9$)
long long int
(usually long long
)
- Memory: 8 bytes
- Range: $-2^{63}$ to $2^{63} - 1$ (roughly $-9 \times 10^{18}$ to $9 \times 10^{18}$)
char
- Actually an integer type, but it is printed as ASCII.
- Memory: 1 byte
- Range: -128 to 127
There are also unsigned variants of these integer types, which can only store non-negative integers.
For example, unsigned int
still uses 4 bytes but its range is $0$ to $2^{32} - 1$.
There is also unsigned char
and unsigned long long
.
We almost never use unsigned integer types.
The actual memory size and range may be larger, depending on the computer and C++ compiler.
Integer Overflow
When the result of arithmetic operations exceeds the integer's range, an overflow occurs.
For example, 2147483647 + 1
gives -2147483648
.
If this occurs, switch to a larger integer type like long long
.
For unsigned integers, unsigned int n = 0; n--;
gives n
a value of 4294967295 ($2^{32} - 1$).
Overflow for unsigned integers works like modular arithmetic — this is discussed in a later lesson.
There are situations where integer overflow computes correct results, but we leave this until we discuss how computers store integers in a later lesson.
Floating-point Numbers
Computers (usually) store decimal numbers in a way similar to scientific notation,
so they can hold extremely small or large values.
Hence, they cannot represent decimal numbers like $\frac{1}{3} = 0.333...$ exactly.
That's why 0.1 + 0.2
evalutes to 0.30000000000000004
in many programming languages.
Do not rely on floating-point arithmetic to compute exact answers (*cough cough D107*).
We have three types of floating-point numbers, in increasing order of precision (and memory size):
float
(4 bytes), double
(8 bytes) and long double
(16 bytes).
Use sizeof
to find the memory size of a data type, e.g. sizeof(long double)
.
Literals
When you specify the value of a data type directly in C++ code, it is called a literal.
int
:123
long long
:12347912384718ll
char
:'A'
float
:1.234f
double
:1.234
or2e9
long double
:1.234l
string
:"abc"
Unit of information / data storage
A binary digit (bit) (symbol: b) is either $0$ or $1$.
- 1 byte: 1B = 8 b
- 1 kilobyte: 1 kB = 1024 B (note that $1024 = 2^{10}$)
- 1 megabyte: 1 MB = 1024 kB
- 1 gigabyte: 1 GB = 1024 MB
- 1 terabyte: 1 TB = 1024 GB
In practice, people (especially hard drive manufacturers) commonly use 1 kB = 1000 B, 1 MB = 1000 kB, etc. An alternate set of binary-prefixed units (Kibibyte (KiB), Mibibyte (MiB), etc) have been proposed to make it clear that the 1024 multiplier is being used, but these units are not in popular use.
Internet service providers (ISPs) like to trick consumers by using bit (b) instead of byte (B). For example, a data transfer rate of 21 Mbps (megabit per second) is actually $2.625$ MBps (megabyte per second).
Authored by s16f22