Data types
We already introduced data types earlier in the variables section.
Here is an example of a code with all the data types used for revision purpose.
#include <iostream>
#include <string>
using namespace std;
int main () {
// Creating variables
int _Num = 5; // Integer
float _Float = 5.99; // Floating point number
double _Double = 9.98; // Floating point number
char _Char= 'G'; // Character
bool _Bool = true; // Boolean
string _String = "Namaste"; // String
// Print variable values
cout << "int: " << _Num << "\n";
cout << "float: " << _Float << "\n";
cout << "double: " << _Double<< "\n";
cout << "char: " << _Letter << "\n";
cout << "bool: " << _Bool << "\n";
cout << "string: " << _String << "\n";
return 0;
}
Space each data type occupies
Int - 4 bytes
Float - 4 bytes
Double - 8 bytes
Char - 1 byte
Bool – 1 byte
Note: Since string is a collection of characters it occupies the space equivalent to that of the addition of the space occupied by each character in that string.
What is the difference between float and Double?
Both of them differ in their size. Float occupies 4 bytes however double occupies 8 bytes. This implies they also differ in the precision they provide. The precision of float is only six decimal digits, while double variables have a precision of about 15 digits. Hence it is safer to use double unless you are concerned about how much memory is being occupied.