Data Types in C++

Variables, as we’ve seen, store information that the programmer can then later call up, and manipulate if required. Variables are simply reserved memory locations that store the values the programmer assigns, depending on the data type used.

The Value Of Data

There are many different data types available for the programmer in C++, such as an integer, floating point, Boolean, character and so on. It’s widely accepted that there are seven basic data types, often called Primitive Built-in Types; however, you can create your own data types should the need ever arise within your code.

The seven basic data types are:

Integer, Floating Point, Character, Boolean, Double Floating Point, Wide Character, No Value.

These basic types can also be extended using the following modifiers: Long, Short, Signed and Unsigned. Basically this means the modifiers can expand the minimum and maximum range values for each data type. For example, the int data type has a default value range of -2147483648 to 2147483647, a fair value, you would agree.

Now, if you were to use one of the modifiers, the range alters:
Unsigned int = 0 to 4294967295
Signed int = -2147483648 to 2147483647
Short int = -32768 to 32767
Unsigned Short int = 0 to 65,535
Signed Short int = -32768 to 32767
Long int = -2147483647 to 2147483647
Signed Long int = -2147483647 to 2147483647
Unsigned Long int = 0 to 4294967295

Naturally you can get away with using the basic type without the modifier, as there’s plenty of range provided with each data type. However, it’s considered good C++ programming practise to use the modifiers when possible.

There are issues when using the modifiers though. Double represents a double-floating point value, which you can use for incredibly accurate numbers but those numbers are only accurate up to the fifteenth decimal place. There’s also the problem when displaying such numbers in C++ using the cout function, in that cout by default only outputs the first five decimal places. You can combat that by adding a cout.precision () function and adding a value inside the brackets, but even then you’re still limited by the accuracy of the double data type. For example, try this code:

#include <iostream>
using namespace std;
double PI = 3.141592653589793238463;

int main()
{
cout << PI;
} Build and run the code and as you can see the output is only 3.14159, representing cout’s limitations in this example. You can alter the code including the aforementioned cout.precision function, for greater accuracy. Take precision all the way up to 22 decimal places, with the following code:

#include <iostream>
using namespace std;
double PI = 3.141592653589793238463;

int main()

{
cout.precision(22);
cout << PI;
} Again, build and run the code; as you can see from the command line window, the number represented by the variable PI is different to the number you’ve told C++ to use in the variable. The output reads the value of PI as 3.141592653589793115998, with the numbers going awry from the fifteenth decimal place.

This is mainly due to the conversion from binary in the compiler and that the IEEE 754 double precision standard occupies 64-bits of data, of which 52-bits are dedicated to the significant (the significant digits in a floating-point number) and roughly 3.5-bits are taken holding the values 0 to 9. If you divide 53 by 3.5, then you arrive at 15.142857 recurring, which is 15-digits of precision.

To be honest, if you’re creating code that needs to be accurate to more than fifteen decimal places, then you wouldn’t be using C++, you would use some scientific specific language with C++ as the connective tissue between the two languages.

You can create your own data types, using an alias-like system called typedef. For example:

#include <iostream>
using namespace std;
typedef int metres;

int main()
{
metres distance;
distance = 15;
cout << “distance in metres is: “ << distance;
} This code when executed creates a new int data type called metres. Then, in the main code block, there’s a new variable called distance, which is an integer; so you’re basically telling the compiler that there’s another name for int. We assigned the value 15 to distance and displayed the output: distance in metres is 15.

It might sound a little confusing to begin with but the more you use C++ and create your own code, the easier it becomes.

David Hayward

David has spent most of his life tinkering with technology, from the ZX Spectrum, getting his hands on a Fujitsu VPP5000/100 supercomputer, and coding on an overheating Raspberry Pi. He's written for the likes of Micro Mart, Den of Geek, and countless retro sites and publications, covering reviews, creating code and bench testing the latest tech. He also has a huge collection of cables.