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
User-Defined Data Types In C Data Types are the types of data that can be stored in memory using a programming language. Basically, data types are used to indicate the type of data that a variable can store. These data types require different amounts of memory and there are particular operations that can be performed on them. T
4 min read
User Defined Data Types in C++ User defined data types are those data types that are defined by the user himself. In C++, these data types allow programmers to extend the basic data types provided and create new types that are more suited to their specific needs. C++ supports 5 user-defined data types:Table of ContentClassStructu
4 min read
COBOL - Data Types A Datatype is a classification by the programmer to tell the compiler/interpreter how data will be used inside a program. For example, the roll number of the student defined as the number will take input as a number only if other values are supplied instead of the number it will raise an abend insid
4 min read
Data Type Justification in COBOL Data Classification/Types helps the system identify how the programmer wants to use the data in any particular program. In COBOL we have three different Classifications of DATA TYPES, which are Numeric, Alphabetic, and Alpha-Numeric data types. In COBOL DATA TYPE justification applies to Numeric dat
4 min read
C++ Compound Data Types Quiz Built-in data types cannot store all the information in an easily accessible and organized way. That is why C++ provides compound data types such as arrays, pointers, strings, etc. that are derived from the built-in data types and provide different way to use them. Good understanding of compound dat
2 min read