Numeric Data Types in C++ Programming

Instructor: Edward Lavieri

Ed has created and taught college courses since 2002 and has a Doctorate of Computer Science and three MS degrees. He has authored several tech books.

In this lesson, we will explore the numeric data types in the C++ programming language. Ranges of each numeric data type will be reviewed as well as their required memory allocation.

C++ Numeric Data Types

C++, pronounced 'cee plus plus,' is a core programming language that untypically makes great use of numeric data types. These numeric data types can be categorized as integers and floating points. Integers are whole numbers and floating points have decimal points. Let's look at each category of numeric data types.

Integers

Integers, referred to in code as int, come in several forms: short int, int, and long. A short int can be any number in the range of -32,768 to 32,767. A regular int, or just int, has a much larger range of -2,147,483,648 to 2,147,483,647.

A long int, referred to as long or double, has a range of 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

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.

cout << 'short int Min/Max : ' << SHRT_MIN << ' / ' << SHRT_MAX << endl;
cout << 'int Min/Max : ' << INT_MIN << ' / ' << INT_MAX << endl;
cout << 'long Min/Max : ' << LONG_MIN << ' / ' << LONG_MAX << endl;

The output is as follows:

short int Min/Max : -32768 / 32767
int Min/Max : -2147483648 / 2147483647
long Min/Max : -9223372036854775808 / 9223372036854775807

Integers can also be signed or unsigned, which refers to, in the case of signed, support for negative numbers. We can use the following code to determine the maximum value of unsigned integers:

cout << 'unsigned short int Max : ' << USHRT_MAX << endl;
cout << 'unsigned int Max : ' << UINT_MAX << endl;
cout << 'unsigned long Max : '<< ULONG_MAX << endl;

The output of this code is as follows:

unsigned short int Max : 65535
unsigned int Max : 4294967295
unsigned long Max : 18446744073709551615

Floating Points

Floating points are the second category of numeric data types in C++. As the name suggests, floating points can have a decimal, which provide a greater level of precision over integers.

There are three types of floating point numbers in C++: float, double, and long double. Floats are the smallest, doubles are twice the size of floats, and long doubles are twice the size of doubles. The code below will generate output to display the minimum and maximum value of the float data types.

cout << '\tFloat \t\t' << 'Double \t\t' << 'Long Double' << endl;
cout << 'Min : ' << FLT_MIN << '\t' << DBL_MIN << '\t' << LDBL_MIN << endl;
cout << 'Max : ' << FLT_MAX << '\t' << DBL_MAX << '\t' << LDBL_MAX << endl;

The output for the preceding code is provided below:

Float Double Long Double
Min : 1.17549e-38 2.22507e-308 3.3621e-4932
Max : 3.40282e+38 1.79769e+308 1.18973e+4932

Memory Allocation

Numeric data types, like other data types, require memory allocation whenever a variable is defined. How much memory is allocated depends on the data type. A good programming practice is to use the smallest data type possible for any given situation to conserve memory.

Memory Requirements

The following C++ code uses the sizeof() operator to determine how many bytes each of the numeric data types require.

To unlock this lesson you must be a Study.com Member.
Create your account

Register for a free trial

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Free 5-day trial

Earning College Credit

Did you know… We have over 160 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it free for 5 days!
Create an account
Support