Hello everyone!
This is my first post on the blog.
So let’s start. Today we will discuss the most basic data types.
There are seven primitive data types:
Data type | Size (bytes) | Describe |
---|---|---|
int | 4 | only integer numbers |
float | 4 | floating-point numbers |
double | 8 | floating-point numbers |
bool | 1 | it represents true or false (1 or 0) |
void | 0 | no value: using with function which returns nothing |
character | 1 | using for letters, also numbers as character |
wide character | 2/4 | larger character |
Each of them consume memory of your computer. It is up to two things: data type
and modifiers
.
There are four data modifiers in C++:
· signed
· unsigned
· short
· long
Signed:
Some ot data type are signed default for example char.
Unsigned:
When you know that you don’t need to negative numbers in your data type you can use keyword unsinged. It has huge advantage:
you can increase the max range of positive number (the range will be from 0 to [max_positive_range + max_negative_range] or [max_positive_range * 2 + 1].
For example if you are calculating the numbers of factorial you know that there are only positive numbers. With keyword you can store more numbers without getting more memory for variable.
Short:
It reduces the range of number of variable and the size of variable.
Long:
Use it when you want to increase range of number: negative and positive numbers range.
C++ allows you to combine the propertly data modifiers
with each others. For example you can write:
long long int or unsigned long long int.
In the table below I present two data types with modifiers and how many memory they consume:
Data type | Size (bytes) | Range |
---|---|---|
short int | 2 | -32,768 to 32,767 |
int | 4 | -2,147,483,648 to 2,147,483,647 |
unsigned int | 4 | 0 to 4,294,967,295 |
long int | 4 | -2,147,483,648 to 2,147,483,647 |
long long int | 8 | -(2^63) to 2^63 |
unsigned long long int | 8 | 0 to 18,446,744,073,709,551,615 |
char | 1 | -128 to 127 |
signed char | 1 | -128 to 127 |
unsigned char | 1 | 0 to 255 |
If you want to see how much of memory takes your variable you cant use ‘sizeof’.
There is a way to see the range of data types too:
Useful links:
There you can read more:
LIMITS
ASCII