“Without data, you are just another person with an opinion” W. Edwards Deming
Before reading this section, we’re expecting you to be familiar with variables. If don’t then please make sure you read the variables section first.
When we create a variable in a program, a portion of the memory space will be allocated to that variable. But why there should be a memory space for a variable? Also how much exactly this memory space is?
Well, programs work with data. Consider a calculator; We first enter a number, then a few milliseconds or a few seconds later we choose the type of mathematical operation (like multiplication) and then put another value and finally hit the enter to get the result.
If you think about it, we don’t set the numbers and the type of operation in the calculator all at the same time! There is some elapsed time between each input.
So it does make sense to store each input until we need them.
This is where variables come in. They represent a memory space that can be used to store these types of values.
Also in a program, there are times that we need to store the result of an operation in a variable because another part of the same program might need that result later on.
So yes, we need variables and each variable need memory space because it’s a place where a value can be stored.
It depends on the type of data!
In C++ programming, there are different types of data. For example integer numbers like 1,2,3,4,53, 2342, 2342,232,34 etc. are of type int. So if we know that a program will going to work with integer number and wants to store such values, we should create a variable of type int.
Note: there are other types of data that also work with integer numbers which we will introduce them later in this section.
An integer (int) type of variables usually have enough memory space that they can store numbers range from -2,147,483,648 to 2,147,483,647. Also an integer-type variable has 4 bytes of memory allocated.
Note: byte is a unit of memory measurement. You can learn about it in bits&bytes section.
You should know that the exact size of a memory space for any data-type is system dependent. For example we said a variable of type int is 4 bytes, but in one system it might be 8 bytes and in another one it might end up taking 2 bytes of memory space.
Also another example would be float numbers which is basically those numbers that has fraction-part. For example: 233.422 or 432.342 or 4442.1134 etc. So if in a program we need to store such value, we need to create a variable of type float or double. A variable that is typed double has enough space to hold about 15 significant digits.
For example:
double dVariable = 12345.6789012345;
double length = 1234567890.12345;
Now that we have a general sense of data-types let’s see how many data types are in C++.
Generally speaking there are two categories of data types supported in C++ language:
-
Custom data types: are those types that developers build via
enum , union, class and structure
keywords. -
Basic data types: these types are built in the core of C++
language and their size and use cases are already declared.
Note: there is also compound data type like arrays, and string that we covered them in later sections.
In this section we will focus on basic data types and custom data types will be covered in enum, union, class and structure sections.
List of basic data types supported in C++ language:
`int`: Integer or `int` is a number with no fraction part. For example 1, 200, -300 etc. are integer and values like 2.3, 4.4, and 543.023 are not.
When a variable is typed int, the amount of space that will be assigned to this variable is big enough to hold the range of values from -2,147,483,648 to 2,147,483,647.
For example:
int number = 400;
int number = 3220;
You might be wondering what will happen if we surpass the limits and assign a value higher or lower than the range of int data-type.
For example:
#include <iostream>
int main() {
int number = 2147483648;
std::cout<<number;
return 0;
}
Result: result: -2147483648
In such case, the compiler will act like a car’s odometer. When the value we set for a data-type surpasses the maximum allowed range for example, the assigned value to the target variable is the amount of number that is passed the limit, added to the minimum allowed range of that data-type. And that’s what we got in this example.
Note: a variable of type int will take 4 bytes of memory space.
`short int` (or just `short`): This is another data-type that accepts integer values (just like `int`) except the range of the values we can put to the variables of type `short` is smaller compared to `int` data-type. Specifically, this range is from -32,768 to 32,767.
Note: the memory size of a short variable is at least 2 bytes.
For example:
short number = 1000;
short int number = 1000;
Note: There’s no difference between using short int or short.
`long int` (or just `long`): This data-type also is used for integers numbers (just like `int`) but compare to `int` data-type, a variable that is typed `long` can hold higher range of numbers. More specifically, this range is from -2,147,483,648 to 2,147,483,647.
Example:
long number = 1000000000;
long int number = 2500000000;
Note: as you can see, there’s no difference between using long int and long as the data-type of a variable.
Also the memory space of this data-type is 8 bytes.
`long long int` (or just `long long`): there are times that we want to store an integer value that none of the data-types introduced so far can handle it. This is where we can use another integer related data-type named `long long`. The range of values that a variable of this type can store is ranged from –9,223,372,036,854,775,807 to 9,223,372,036,854,775,807.
Example:
long long number = 1000000000;
long long int number = 2000000000;
`char`:
If for any reason in a program we needed to store characters, we can use this data-type. Characters can be letters, numbers, and punctuations. But remember, to handle characters, computers use numerical codes in which certain integer numbers represent certain characters. And for this reason, characters are stored as integers in memory when assigned to a variable that is typed char.
For example an integer value 65 represents the character A.
char character ='A';
char character2 =65;
In the example above, even though it seems each variable has different value, behind the scene the real stored value in the memory for both variables will be the binary code of 65.
Notes:
-
Any character that we assign to this type of variables should be
between two single quotations `’ ‘` like the way example above
showed. -
If you decided to use numbers instead of characters as the value
of `char` variables, you should know that the range is only from -128 to
127. Also there’s no need to use single quotation mark if you’re using
the number code of a character. But if you want to store the number
itself into a variable of type `char`, then in that case you need to use
single quotations as well.
Example:
#include <iostream>
using namespace std;
int main()
{
char cNumber1 = 1;
char cNumber2 = '1';
cout<<cNumber1<<", "<< cNumber2<<endl;
return 0;
}
Output:
☺, 1
The variable named cNumber1 stored the value 1 which is the numerical code of smiley face but the cNumber2 stored the character-value 1 because put this value within two single-quotations.
So when the program ran we got the smiley face as the value stored in the cNumber1 and for the value stored in the cNumber2 we got 1.
-
The memory size of a char variable is 1 byte.
`unsigned`:
All the data types mentioned above can take negative numbers as well. Basically they’re signed data-type by default.
Note: `signed` data-type refers to those types that accept negative numbers as well.
For example there’s no difference between:
`signed int = -10;`
And:
`int = -10;`
Because the default behavior of these data-types is to accept negative numbers as well, we can skip the use of signed keyword.
But if we know that a variable will not take any negative numbers, we can then use unsigned data-type-name variable-name structure.
In this case the variable no-longer capable of taking negative numbers. But on the positive side the variable of unsigned data-type is capable of taking higher range of positive values.
Here’s the list of unsigned data-types and the range of values they can take:
| Data Type | Range |
|---|---|
| unsigned char | 0 to 255 |
| unsigned int | 0 to 4,294,967,295 |
| unsigned short | 0 to 65,535 |
| unsigned long | 0 to 4,294,967,295 |
| unsigned long long | 0 to 18,446,744,073,709,551,615 |
| unsigned char | 0 to 255 |
Note: the memory size of each unsigned data-type is equally the same as what it was without unsigned.
`float`: float is a data-type that allows numbers with fraction-part to be stored in the variables of this type. A variable typed `float` has enough memory space that can store values with 7 significant digits.
Example:
float fVariable = 123.4567;
float fVar = 1234.321;
float fVar2 = 1.23456;
Note: the memory space of a variable typed `float` is at least 4 bytes.
`double`: double is another data-type that allows numbers with fraction-part to be stored in the variables of this type. A variable typed `double` has enough memory space that can store values with 15 significant digits.
Example:
double dVar1 = 1234567890.12345;
double dVar2 = 1.23456789012345;
double dVar3 = 12.3456789012345;
Note: the memory space of a variable typed `double` is at least 8 bytes.
`long double`: when we want to store numbers with fraction-part that have more than 15 significant digits, we can use a variable of type `long double`. Depending on the underlying system, a variable typed `long double` can hold a value ranged from 15 up to 32 significant digits.
Not: the memory space of a variable types `long double` is between 8 to 16 bytes.
`bool`: this data-type is used to create a variable that can hold the keywords `true` or `false`. Behind the scene the keyword `true` represent the value `1` and the keyword `false` represent the value `0`. This means we can also put the value 1 as `true` and 0 as `false` to a variable of type `bool`.
Note: actually we can put any non-zero value in a variable of this data-type and it will be considered as true.
Example:
bool yes = true;
bool no = false;
bool falsy = 0;
bool tr = 1;
Boolean variables are mostly used in conditional statements like if-statement, while-loop, do-white loop etc.
Constant values:
Those types of values that are not wrapped by a variable are called constant values.
For example: 2.3 or 2.555, 2, 34234, 3452342 etc.
The type of these variables depending on their size is decided automatically by the compiler.
For example constant fractioned values like 2.3, 3.33 etc. are of type double by default , so if we want to represent them as float type, we need to use the word f or F after the number. Like: 2.3f or 3432.23423F etc.
Example:
float number = 43.34533f;
float number2 = 532.334F;
But assigning a fractioned number with or without the character f to a variable doesn’t have any differences because we already declared the type of variable and so it’ll be float in this example no-matter what.
So is there any use case in using the character f?
The answer is definitely yes as you’ll see in the example below:
#include <iostream>
using namespace std;
int main() {
float number= 2.333;
if (number == 2.333){
cout<< "Yes, they are equal n";
}else{
cout<<"No, they are not equaln";
}
return 0;
}
Result: No, they are not equal
Note: here we’ve used if statement, if you’re not familiar with this statement, please read the if-else statement section.
In the example above, even though the two numbers are the same or at least this is what it looks like, the result is negative and according to C++ language, these two numbers are not equal!
This is because the number 2.33 is a constant of type double and even though this might not be apparent at first but it took more memory space than the number variable of type float and as a result there’s no equality between their amount of space in memory.
You can see this yourself by running the example below:
#include <iostream>
using namespace std;
int main() {
float number= 2.333;
if (number < 2.333){
cout<<"Yes, number-variable is less than 2.33 n";
}else{
cout<<"No, number-variable is not less than 2.33n";
}
return 0;
}
Result: Yes, number-variable is less than 2.33
Now if we add the character f to the constant value as 2.33f then in that case the type and values of both variable and the constant will be equally the same.
#include <iostream>
using namespace std;
int main() {
float number= 2.333;
if (number == 2.333f){
cout<<"Yes, they are equal n";
}else{
cout<<"No, they are not equal n";
}
return 0;
}
Result: Yes, they are equal
Also for integer constant values, the default type is int.
For example if you represent a value like 1000 this value will be stored in the memory as integer.
But we can change the type of an integer constant to other types like long by adding either lor L character in front of the value.
For example:
2344L
23L
3342l
4422l