C++ Fundamental Datatypes#
reading
Fundamental types from cppreference.com
Data is stored in computer memory. We’ll refer to the data in memory as an object.
We can access this data:
directly: using a descriptive object name
indirectly: using a pointer or a reference to the memory location (more on this later…)
Data stored in the object can be:
Here we’ll see how do declare objects using descriptive names.
Memory#
The smallest unit of storage is the bit, simply a 1 or 0.
This is something that can be expressed by the hardware in your computer.
Computer memory is usually thought about in terms of bytes, with the standard (today) of 1 byte = 8 bits.
We tend to use the SI prefixes for these, so 1 kilobyte = 1000 bytes.
But your filesystem will likely use base-2 prefixes, so 1 kibibyte (KiB) = 1024 bytes.
Important
When we want to store something in C++, we need to tell the compiler what type of data we are storing, and that will in turn tell it how many bytes of memory to allocate for the data.
Types#
There are several basic types of data:
bool: a 1-bit true or falsechar: a 1-byte character (like'a')int: an integerfloat: a single precision floating point numberdouble: a double precision floating point number
Note
A floating point number is any
number with a decimal point, like 2.0 or 1.e-20.
Generally, floating point numbers cannot be exactly represented on a computer, since there are infinite numbers on the number line between \([0, 1]\), but the computer only has a finite amount of memory.
Tip
Single and double precision refer to how much memory (and therefore how much precision) is used to store the numbers. Usually single precision uses 32 bits and double precision uses 64 bits.
There are also modifiers that can be used with many of these like: short, unsigned, long.
A table of C++ datatypes is provided in the CPlusPlus tutorial variables section
Scientific notation#
We write numbers in scientific notation like \(5.2 \times 10^{11}\). We’ll refer to the \(5.2\) as the significand, and the \(11\) as the exponent, so a floating point number is written as:
On a computer (as with your calculator), we use the e character to separate the sigificand
and exponent, so \(5.2\times 10^{11}\) would be written as 5.2e11.
Defining and initialization#
We can define a variable as:
int i;
float x;
double slope;
This tells the computer how much memory it needs to reserve for each of these objects.
Tip
C++ variable names are case-sensitive, so x and X are distinct.
Warning
Variable names can include letters, numbers, and _, but cannot
begin with a number. Additionally, there are some reserved
keywords (like int) that cannot be used as variable names.
Assignment#
To assign a value to an object, we need to use the assignment operator, =.
E.g.,
i = 1;
Initializing#
There are a few ways to initialize. Here are the two we’ll see the most:
int i = 0;
double x{};
Both of these will initialize the variable to 0. The {}
notation is more flexible, since it will work with more complicated
objects that we will encounter later.
Important
It is always a good idea to initialize a variable with a starting value. Otherwise, most compilers will leave it undefined and you can run into problems if you try to use it without first remembering to initialize it.
We’ll see a third way to initialize objects when we look at classes (and constructors).
Caution
Consider the following code:
#include <iostream>
int main() {
double x;
double y = 10;
std::cout << x + y << std::endl;
}
Here we are using x without initializing it. This behavior is
undefined, and can lead to errors that can be hard to track down.
const#
Objects defined with const cannot be changed, so you are required
to initialize the immediately:
const double G = 6.67e-8;
Try it…
Let’s write an example using a const variable and try to modify it.
What happens?