data types inC++
Anything you deal with inC++It has a specific type and has a special way to deal with it; For example, numbers have a way to deal with them, texts have a way to deal with them, functions have a way to deal with them, and so on.
inC++There are simple and ready-made data types that you can deal with. We will tell you about them in detail in this lesson, and there are other ready-made data types that you will learn later in the course because you are not ready for them yet. You can also define new types and this matter you will learn later as well.
data types inC++
data inC++It is divided into three main sections, as shown in the following image.
The basic data types that we will learn about and start dealing with from now on are the primary types(Primary)Which is the same in most programming languages.
We would like to point out that we will learn about all the other types mentioned later in the course when we are ready for them.
Important note
You may find other names for data types, and this is a very natural thing, for example, you may find primary data types(Primary)They are called primitive types(Basic Data type)or types that come directly within the language(Built-in)And other than that, but the idea is the same and these are all differences that are not important at all.
Primary data types inC++
The most important types of primary data inC++The most used of them are:
int
- float
- double
- bool
-char
type int
in C++
This type is used to store an integer, i.e. a number that does not contain a decimal point.
Example
int x = 10;
type float
in C++
This type is used to store a number that can contain a decimal point.
This number can have 7 digits after the comma.
Example
float x = 12.5;
type double
in C++
This type is used to store a number that can contain a decimal point.
This number can have up to 15 digits after the comma, so it is more accurate than the type float
for finer arithmetic operations.
Example
double x = 12.5;
type bool
in C++
This type is used to store either the value true
or the valuefalse
.
Example
bool x = true;
type char
in C++
This type is used to store a foreign character or an integer whose value is a letter relative to the ASCII code(ASCII Code)his own.
For example char x = 65;
, exactly the same if you write char x = 'A';
.
The reason why the number is transformed 65
into a letter 'A'
in memory is that it is in a systemASCIIThe number 65
represents the letter .'A'
.
Example
char x = 'A';
note
The type char
can store any foreign character without problems because the foreign character needs space1ByteFrom memory only until it is stored.
While the Arabic letter needs space2Bytesof memory until it is stored properly.
For this reason, when dealing with Arabic characters, it is recommended to store one character as an array of type char
consisting of two elements, or as plain text of its typestring
.
You don't have to worry about storing the vowels because what we just mentioned will pass you through in later lessons.
Define properties of data types inC++
inC++There is a group of words that sayData Type ModifiersYou can use it to specify the properties of the values that can be stored, such as whether or not values can be less than zero, as well as to increase the amount of space each type is allocated in memory, making you able to store larger values.
Types char
and properties of int
the double
values that can be stored in them can be specified if we add with them one of the following words:
signed
We add it to one of the previous types if we want to determine that the value of the variable does not matter if it is greater, less than, or equal to zero.unsigned
We add it to one of the previous types if we want to specify that the value of the variable cannot be less than zero.short
We add it to one of the previous types if we want to specify that the amount of memory that will be allocated to the variable is2Byte.long
We add it to one of the previous types if we want to specify that the amount of memory that will be allocated to the variable is8Byte.
Technical information
By default types char
- int
- float
- double
are all considered signed
even if you don't define it yourself that's why you can store values less than zero in them.
Characteristics of data types in C++
The following table shows the space each type needs in memory and the values that can be stored in it, whether the type is used alone or its properties are specified.
Type | size in memory | The value that can be stored |
---|---|---|
char |
1Byte | between -128 and127
|
unsigned char |
1Byte | between 0 and255
|
short int |
2Bytes | between -32768 and32767
|
unsigned short int |
2Bytes | between 0 and65535
|
int |
4Bytes | between -2147483648 and2147483647
|
unsigned int |
4Bytes | between 0 and4294967295
|
long int |
4Bytes | between -2147483648 and2147483647
|
unsigned long int |
4Bytes | between 0 and4294967295
|
long long int |
8Bytes | between -9223372036854775808 and9223372036854775807
|
unsigned long long int |
8Bytes | between 0 and18446744073709551615
|
float |
4Bytes | between 1.17549e-038 and3.40282e+038
|
double |
8Bytes | between 2.22507e-308 and1.79769e+308
|
long double |
12Bytes | between 3.3621e-4932 and1.18973e+4932
|
If you want to use any type with its properties defined, you can use any type mentioned in the table as follows.
Example
// short int here we used the type short int x = 4545; // long long int Here we used the type long long int y = -50050012; // unsigned long int Here we used the type unsigned long int = 880045023;
There are many ready-made types that you can use, but we touched on the most famous and most used among them. We would like to point out that the characteristics of the mentioned types may differ slightly depending on the compiler used to interpret the language codeC++For a computer but this is not something you have to worry about especially at this point.
In the next lesson, you'll learn all the techniques you can use to define variables using the data types we learned in this lesson.