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
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;
}
Outputsizeof 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;
}
Outputsizeof 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;
}
Outputsizeof 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;
}
Outputvalue 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++
Similar Reads
Numeric Types in MATLAB Numeric class in MATLAB includes signed and unsigned integers, single-precision floating-point numbers, and double-precision floating-point numbers. Generally, MATLAB stores all numeric values as double-precision floating-point. But, we can choose to store any number, or, an array of numbers, as int
3 min read
Data Type Modifiers in C In C, data type modifiers are the keywords used to modify the original sign or length/range of values that various primitive data types hold such as int, char, and double.Let's take a look at an example:C#include <stdio.h> int main() { // Using unsigned int and trying // to store negative valu
4 min read
C++ Data Types Data types specify the type of data that a variable can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data type with which it is declared as every data type requires a different amount of memory.C++ supports a wide variety of data typ
7 min read
R Data Types Data types in R define the kind of values that variables can hold. Choosing the right data type helps optimize memory usage and computation. Unlike some languages, R does not require explicit data type declarations while variables can change their type dynamically during execution.R Programming lang
5 min read
Data Types in Objective-C In Objective-C, each variable has an associated data type and each data type has different amounts of memory required in a system. A data type is a classification of data that tells the compiler or interpreter how the programmer intends to use the data. Or in other words, a data type represents what
8 min read