C++ Data types

When writing a program, there may come a time when you need to store some values for later use. The place where information or data is stored is called a variable. A variable is like a labeled box where we can put something.
Before giving you the formal definition of a variable, we need to talk about how the memory in your computer is structured. The memory can be thought of as a sequence of blocks stacked one on top of the other (Figure 1.) and every single block has two values of interest:
  • Its address – with this value you can access the block
  • its content – the value which is stored inside the actual block of memory.
memory.drawio (1)

Figure 1. Memory structure

mem_example.drawio (1)

Figure 2. Example of a variable in memory

Variables in that case would be a sequence of memory blocks. Accessing a variable is the same as accessing the memory location and the value stored in the variable is the same as the value stored in that memory location. As an example, in Figure 2. a variable named “x” which has the value 10 stored in it is in memory location 0x1234.
One more detail we need to point out is the definition we gave: “Variables are sequences of memory blocks“. The size of each memory location is fixed, so if you want to store a variable which can fit inside the memory block, you can, but what happens when your variable is larger than the block? For that reason, variables which need more space are split and the pieces are stored in adjacent memory locations. Users don’t need to worry about that detail.
Finally, there are two types of variables in C++: Primitive and non-primitive. The difference between these is explained in the further text.

Primitive data types

Before we get into the definition of primitive data types, we need to discuss what does data type mean. C++ is called a strictly-typed language. That means that every time you define a variable, it can only store a specific type of data. For example, in Figure 2. “x” is storing value 10, and 10 is an integer. If for example in later use we wanted to store the word “Dog” inside “x”, we couldn’t, because “Dog” is not an integer. So we come to the result that data types are specific formats which every variable must have. The strictly-typed means that once a variable is assigned with its data type, it cannot be changed later.
The basic types, or primitive types which are defined in C++:
  • integer – 32 bits
  • long – 64 bits
  • float – 32 bits
  • double – 64 bits
  • char – 8 bits
  • bool – 8 bits
  • string – A sequence of chars, 8 bits x number of chars in a string
int x = 32; //Integer "x" storing "32"
long y = 10; //Long "y" storing "10"
float z = 3.5; //Float "z" storing "3.5"
char character = 'c'; //Char "character" storing "c"
bool condition = true; //Bool "condition" storing "true"

These types follow the same definition as the one given for the variable. For example, let’s say that the size of a memory location in our memory is 8 bits or 1 byte. Chars are data types which are 8 bits long, so every single char will be stored in exactly one memory location. Integers, on the other hand are 32 bits long, so every single integer will be stored in four adjacent memory locations.

Non-primitive data types

When it comes to non-primitive data types, there are two categories:
  • Derived
  • User-defined
For now, we will only cover one non-primitive derived data type – arrays. The same way bigger primitive data types are stored in a sequence of adjacent memory location, arrays are essentially sequences of variables of the same type stored in adjacent memory locations. For example, we could have an integer array a which stores 3 integers. The way we access these specific integers is using something called an indexing operator. Loosely explained, it is a set of square brackets after the name of the array and inside the brackets is the index of the variable we want to read from/write to. One more thing to point out – C++ is a zero-index language. That means that the first element in the array does not have index 1, but index 0.
int a[3] = {4, 7, -3}; // Array "a" of three integers 4,7 and -3
string dinosaurs[2] = {"Tricerotops", "Steganosaurus"}; // Array of two strings

int number = a[0]; // Variable "number" is equal to 4

int x = a[3]; // Error. There are indexes 0, 1 and 2.

string dino = dinosaurs[1]; // "dino" is equal to "Steganosaurus"

Leave a Reply