Web Tutor

Other Entries

Numeric Data Types in C++

In this tutorial, we are going to explore numeric data types in C++ programming language.
C++ is a core programming language that makes great use of numeric data types. These numeric data types can be categorized as integers(whole numbers) and floating points. Let us look at each category of numeric data type.
 
Integers
Integers, referred to, in code a int, come in several forms. A short int can be any number in the range of -32,768 to 32,768. A regular int, or just int, has a much larger range of 2,147,483,647 to 2,147,483,647.
A long int, referred to us long or double has a range of 9, 223, 372, 036, 854, 775, 808 to 9, 223, 372, 036, 854, 775, 808.
It is important to realize that there is no single standard for numeric data types in C++ and the names and range values can be slightly different depending on the implementation and processor. What is provided in this lesson are the prevailing ranges.
We can execute the following lines of code to determine the range of each data type.
#include <iostream>
using namespace std;

int main()
{
  cout<<"short int min/max: "<<SHRT_MIN<<'/'<<SHRT_MAX<<endl;
  cout<<"int min/max: "<<INT_MIN<<'/'<<INT_MAX<<endl;
  cout<<"Long int min/max: "<<LONG_MIN<<'/'<<LONG_MAX<<endl;
  return 0;
}

Output

450_7228310c48357abc2f716351329bb1c2.png

Floating point

Floating point have a decimal point which provides a greater level of precision over integers.

There are three types of floating point numbers in C++. float(the smallest), double(twice the size of float), long double(twice the size of double).

The following code will generate output to display the minimum and maximum value of the float data type. 

#include <iostream>
#include <limits>
using namespace std;

int main()
{
  cout<<"Float min/max: "<<numeric_limits<float>::min()<<'/'<<numeric_limits<float>::max()<<endl;
  cout<<"Double min/max: "<<numeric_limits<double>::min()<<'/'<<numeric_limits<double>::max()<<endl;
  cout<<"Long double min/max: "<<numeric_limits<long double>::min()<<'/'<<numeric_limits<long double>::max()<<endl;
  return 0;
}

 

Output

450_f5652ce163fd26c26fd98610dcdbb5b0.png

 

Posted in C++ on November 08 2022 at 10:52 AM

Comments (0)

No login