30% Therapy – 40% Practice – 30% Work project

C++ Numeric Data Types



Numeric data types in C++ are used to handle numerical data like integers (both signed and unsigned), floating-point values, and precision values.

Numeric data types mainly contain three fundamental types of numeric data, which are as follows −

  • Int
    • Int
    • Short Int
    • Long Int
    • Long Long Int
    • Unsigned Int
    • Unsigned Short Int
    • Unsigned Long Int
    • Unsigned Long Long Int
  • Float
  • Double
  • Long Double

In this article, we will go through all of the numeric data types and their subtypes in detail with examples.

int Data Type

The int data type is short for integer, which takes numeric values from -231 to (231-1). It takes 4 Bytes (i.e., 32 bits) in the memory.

Syntax

int variable_name;

It is further classified into various derived subtypes, which are as follows −

(a) short int

The short int is used for smaller numbers, as it takes only 2 Bytes (i.e., 16 bits) in the memory. It”s value ranges from -215to (215-1).

Syntax

short int varianle_name;

(b) long int

The long int is used for bigger numbers, with memory space of 4 bytes (i.e., 32 bits), and can be up to 8 bytes (i.e., 64 bits), depending upon the compiler.

Syntax

long int variable_name;

(c) long long int

The long long int is used for larger numbers, with memory space of 8 bytes (i.e. 64 bits), and can be up to 16 bytes (i.e., 128 bits), depending upon the compiler. It”s value ranges from -263 to (263-1).

Syntax

long long int variable_name;

(d) unsigned int

The unsigned int is used to store only non-negative value, and take up same memory space as an int data type, which is 4 Bytes (i.e. 32 bits). It”s value ranges from 0 to (232-1).

Syntax

unsigned int variable_name;

(e) unsigned short int

The unsigned short int is used to store only non-negative value, and take up same memory space as a short int data type, which is 2 Bytes (i.e., 16 bits). It”s value ranges from 0 to (216-1).

Syntax

unsigned short int variable_name;

(f) unsigned long int

The unsigned long int is used to store only non-negative value, and take up same memory space as a long int data type, which ranges from 4 Bytes (i.e., 32 bits) to 8 Bytes (i.e., 64 bits).

Syntax

unsigned long int variable_name;

(g) unsigned long long int

The unsigned long long int is used to store only non-negative value, and take up same memory space as a long long int data type, which ranges from 8 Bytes (i.e., 32 bits) to 16 Bytes (128 bits). It”s value ranges from 0 to (264-1).

Syntax

unsigned long long int variable_name;

Example of int Data Type

The following code shows the size and use of all derived int data types −

#include <iostream>
using namespace std; 

int main() {   

   int a=16;
   short int b=3;
   long int c= -32;
   long long int d= 555;

   unsigned short int e=22;
   unsigned int f=33;
   unsigned long int g=888;
   unsigned long long int h=444444;

   cout << "sizeof int datatype is: " << sizeof(a) 
      <<" and the number is: "<<a<< endl; 

   cout << "sizeof short int datatype is: "
      << sizeof(unsigned int) <<" and the number is: "<<b<<  endl; 

   cout << "sizeof long int datatype is: "
      << sizeof(short int) <<" and the number is: "<<c<<  endl; 

   cout << "sizeof long long int datatype is: "
      << sizeof(unsigned short int) <<" and the number is: "<<d<<  endl; 

   cout << "sizeof unsigned short int datatype is: "
      << sizeof(long int) <<" and the number is: "<<e<<  endl; 

   cout << "sizeof unsigned int datatype is: "
      << sizeof(unsigned long int) <<" and the number is: "<<f<<  endl; 

   cout << "sizeof unsigned long int datatype is: "
      << sizeof(long long int) <<" and the number is: "<<g<<  endl; 

   cout << "sizeof unsigned long long int datatype is: "
      << sizeof(unsigned long long int) <<" and the number is: "<<h<<  endl; 

   return 0; 
}

Output

sizeof int datatype is: 4 and the number is: 16
sizeof short int datatype is: 4 and the number is: 3
sizeof long int datatype is: 2 and the number is: -32
sizeof long long int datatype is: 2 and the number is: 555
sizeof unsigned short int datatype is: 8 and the number is: 22
sizeof unsigned int datatype is: 8 and the number is: 33
sizeof unsigned long int datatype is: 8 and the number is: 888
sizeof unsigned long long int datatype is: 8 and the number is: 444444

float Data Type

The float data type is used for floating-point elements, which are numbers that are followed by a decimal part. This data type takes 4 Bytes (i.e., 32 bits) of memory.

Syntax

float elem_name;

Example of float Data Type

The following code shows the size and use of all derived int data types −

#include <bits/stdc++.h>
using namespace std; 

int main() {

   float k=1.120123;
   cout << "sizeof float datatype is: "<<sizeof(k)<<" and the element is: "<<k<<endl; 
   return 0; 
}

Output

sizeof float datatype is: 4 and the element is: 1.12012

double Data Type

The double data type is used to store floating-point elements with double precision as compared to float. This data type takes 8 Bytes (i.e., 64 bits) of memory.

Syntax

double elem_name;

The double data type has a further categorization which takes up more memory and stores more precise elements.

long double

Another data type derived from double is long double, which takes 16 Bytes (i.e., 128 bits) of memory space.

Syntax

double elem_name;

Example

The following code shows the size and use of all derived int data types −

#include <bits/stdc++.h>
using namespace std; 

int main() {

   double m=1.34000123;
   long double n=1.21312312421;

   cout << "sizeof double datatype is: "<<sizeof(m)<<" and the element is: "<<m<<endl; 
   cout << "sizeof long double datatype is: "<<sizeof(n)<<" and the element is: "<<n<<endl;
   return 0; 
}
Output
sizeof double datatype is: 8 and the element is: 1.34
sizeof long double datatype is: 16 and the element is: 1.21312

Explicit Conversion of Numeric Data Types

In C++, the explicit type conversion is not done automatically, you need to convert the type manually by placing the target data type in the parentheses (). It tells the compiler to convert the value of a variable or an expression to a specific (target) data type.

Example

The following program shows the explicit conversion of numeric data types from one to another −

#include <bits/stdc++.h>
using namespace std;

int main() {

   double a=6.551555; 
   cout<<"value of a (int) is "<<(int)a<<endl;
   cout<<"value of a (float) is "<<(float)a<<endl;
   cout<<"value of a (double) is "<<a<<endl;
   cout<<"Value of a (long double) is "<<(long double)a; 
   return 0; 
}

Output

value of a (int) is 6
value of a (float) is 6.55156
value of a (double) is 6.55155
Value of a (long double) is 6.55155
Translate »