C++ Numeric Data Type
There are mainly 3 types of Numeric Data Types in C++
- int
- unsigned int
- short int
- unsigned short int
- long int
- unsigned long int
- long long int
- unsigned long long int
- float
- double
- long double
1. Integer (int)
An integer is a type of datatype that can store integer values. Integer acquires 4 bytes in memory and ranges from -2147483648 to 2147483647.
Syntax:
int var_name;
There are some data types derived from Integer these are:
i. Unsigned int:
Unsigned integer is a data type with no negative values of integers so the limit is from 0 to 4,294,967,295. All the memory of 4 bytes is used for storing positive values so the limit of an integer is more than that of an int.
Syntax:
unsigned int var_name;
ii. Short int:
A short int is a data type with lesser memory space than integer with 2-byte memory. It has a range from -32,768 to 32,767.
Syntax:
short int var_name;
iii. Unsigned short int:
Unsigned short int is a data type with no negative values of integers so the limit is from 0 to 65,535. All the memory of 2 bytes is used for storing positive values so the limit of an integer is more than that of a short int.
Syntax:
unsigned short int var_name;
iv. long int
long int is a data type with more memory space than integers with 4 bytes and can be up to 8 bytes depending upon the compiler itself.
Syntax:
long int var_name;
v. unsigned long int
unsigned long int is a data type with all positive values. Its size is the same as of long int.
Syntax:
unsigned int var_name;
vi. long long int
long long int is a data type with a memory of 8 bytes and can vary up to 16 bytes depending upon the compiler itself. The range of a long long int is -(2^63) to (2^63)-1.
Syntax:
long long int var_name;
vii. unsigned long long int
unsigned long long int is a data type with memory the same as of long long int with all the positive values inside it. So, the range of unsigned long long int is 0 to (2^64).
Syntax:
unsigned long long int var_name;
Example:
C++
// C++ Program to check size // of Integer data-types #include <iostream> using namespace std; int main() { cout << "sizeof int datatype is: " << sizeof ( int ) << endl; cout << "sizeof unsigned int datatype is: " << sizeof (unsigned int ) << endl; cout << "sizeof short int datatype is: " << sizeof ( short int ) << endl; cout << "sizeof unsigned short int datatype is: " << sizeof (unsigned short int ) << endl; cout << "sizeof long int datatype is: " << sizeof ( long int ) << endl; cout << "sizeof unsigned long int datatype is: " << sizeof (unsigned long int ) << endl; cout << "sizeof long long int datatype is: " << sizeof ( long long int ) << endl; cout << "sizeof unsigned long long int datatype is: " << sizeof (unsigned long long int ) << endl; return 0; } |
sizeof int datatype is: 4 sizeof unsigned int datatype is: 4 sizeof short int datatype is: 2 sizeof unsigned short int datatype is: 2 sizeof long int datatype is: 8 sizeof unsigned long int datatype is: 8 sizeof long long int datatype is: 8 sizeof unsigned long long int datatype is: 8
2. Floating Point (float)
Float is a type of datatype that can store Floating-point values. A float datatype acquires 4 bytes in memory.
Example:
C++
#include <bits/stdc++.h> using namespace std; int main() { cout << "sizeof float datatype is: " << sizeof ( float )<<endl; return 0; } |
sizeof float datatype is: 4
3. Double (double):
Double is a type of data type used for storing double-precision floating-point values or decimal values. A double datatype acquires 8 bytes in memory.
long double: There is another data type derived from double that is long double. It can acquire 12 bytes of memory space.
Example:
C++
#include <iostream> using namespace std; int main() { cout << "sizeof double datatype is: " << sizeof ( double ) << endl; cout << "sizeof long double datatype is: " << sizeof ( long double ) << endl; return 0; } |
sizeof double datatype is: 8 sizeof long double datatype is: 16
Another Example Program to depict all three numeric datatypes together:
C++
#include <bits/stdc++.h> using namespace std; int main() { int a=5; float f=1.5; double d=6.5; cout<< "value of a is " <<a<<endl<< "value of f is " <<f<<endl<< "value of d is " <<d<<endl<< "Value of c is " <<c; return 0; } |
value of a is 5 value of f is 1.5 value of d is 6.5 Value of c is g
To Know more about data types refer to the article – Data Types in C++