As we have seen in the Data Types topic, there are two types of Numbers supported by C++. Integers and floating point numbers.
Integers are the whole numbers (i.e. 7 or 5 or 2.14).
Any whole number(Be it -4 or 45) can be represented by the Int datatype
Integers are the whole numbers (i.e. 7 or 5 or 2.14).
Below are the data types used for Integers.
The Data Type for a whole number is int.
The Int data type is used to store whole numbers from -32,768 to 32,767. And takes 2 bytes of memory.
#include <iostream> using namespace std; int main() { int x = -30535; cout << x; return 0; }
The Data Type for unsigned number is unsigned in. i.e. unsigned int Data Type can hold positive numbers.
The Int data type is used to store whole numbers from 0 to 32,767.
And takes 2 bytes of memory.
#include <iostream> using namespace std; int main() { unsigned int x = 30535; cout << x; return 0; }
The Data Type for signed number is signed int. i.e. unsigned int Data Type can hold positive and negative numbers.
The signed int data type is used to store whole numbers from -32,768 to 32,767.
And takes 2 bytes of memory.
#include <iostream> using namespace std; int main() { signed int x = -30535; cout << x; return 0; }
Say, we have a large value 3372036854775808.
Well! In this case int data type will not be able to hold that value.
And in this scenario, we need a long data type.
And takes 4 bytes of memory.
#include <iostream> using namespace std; int main() { long x = 3372036854775808; cout << x; return 0; }
So, in the above example, we have created a variable x of type long. And assigned a large value 3372036854775808 to it.
The Data Type for unsigned long int number is unsigned long int. i.e. unsigned long int Data Type can hold positive numbers.
#include <iostream> using namespace std; int main() { unsigned long int x = 3372036854775808; cout << x; return 0; }
The short Data Type is used to hold whole numbers between -32,768 to 32,767.
Say, there is a number i.e. 2768. So, to save space we can use Short Data Type.
#include <iostream> using namespace std; int main() { short x = -2768; cout << x; return 0; }
The unsigned short Data Type is used to hold whole numbers between 0 to 32,767.
Say, there is a number i.e. 2768. So, to save space we can use unsigned short Data Type.
#include <iostream> using namespace std; int main() { unsigned short x = 2768; cout << x; return 0; }
A float data type is used to hold floating point numbers. And is used to hold the floating point numbers of 4 bytes.
For example 5.987.
#include <iostream> using namespace std; int main() { float x = 5.987; cout << x; return 0; }
So, in the above example, we have created a variable x of type float. And assigned the value 5.987 to it.
float x = 5.987;
A double data type is used to hold floating point numbers. And is used to hold the floating point numbers of 64 bit.
It is almost same as Float data type. Just that it can hold a floating point number of a larger size.
For example 5.98743.
#include <iostream> using namespace std; int main() { double x = 5.98743; cout << x; return 0; }