Lesson 4 - More on the C++ type system: Data types

C and C++ C++ Basics More on the C++ type system: Data types

In the previous lesson, Variables and the type system in C++, we learned basic data types of C++ (int, double, and string). In today's tutorial, we're going to look at them in more detail and explain how to use them properly. Today is going to be more theoretical, and the next lesson will be very practical. At the end, we'll make a few simple examples.

C++ recognizes many kinds of datatypes, the basic ones are called fundamental and they're mostly numeric.

Whole-number data types

Let's look at the table of all of the built-in whole-number data types in C++, notice the int type, which we're already familiar with.

Data type Range Size
signed char -128 to 127 8 bits
unsigned char 0 to 255 8 bits
short –32 768 to 32 767 16 bits
unsigned short 0 to 65 535 16 bits
int –2 147 483 648 to 2 147 483 647 32 bits
unsigned int 0 to 4 294 967 295 32 bits
long int size and range depend on the compiler
unsigned long int size and range depend on the compiler
long long long int size and range depend on the compiler
unsigned long long int size and range depend on the compiler

Note: The ranges and sizes of data types depend on the compiler you're using and on the system architecture, I've provided values for Visual Studio and 64-bit architecture.

By now, you might be thinking - why do we have so many data types for storing numbers? The answer is simple, we choose which one to use based on the number's size. If the number is large, it consumes more memory. For a user's age, we should select unsigned char since nobody can live more than 255 years. Imagine a database with millions of users on some informational system. If we chose int instead of unsigned char, it would occupy four times more space. Conversely, if we have a function that calculates a factorial, the range of int would not be enough for us and we'd have to use long long.

Notice the unsigned keyword before some of the types. Those types are almost the same as their "twins" without the unsigned keyword, however, they don't store negative values. Therefore, we're able to store a number twice as large in the positive part. The ones that can't store negative values are called unsigned and the ones that can are called signed variables (signed in the sense that it "stores" the positive or negative sign). We don't have to provide the signed keyword, the compiler does that by default (the only exception is char since it carries other meaning).

Nonetheless, you don't have to think hard about the choice of data type, we'll use int almost every time. You have to think about it only in case the variables are in an array or collection in general, and there are a lot of them. In that case, it's worth it to consider memory requirements. The tables I gave here are mainly for the sake of completeness. The already-mentioned implicit conversion also works between the types, so we can assign an int to a long variable directly, without having to convert it.

Decimal numbers

For decimal numbers, the choice is simpler. We can only choose between three data types. They differ in the range of values, and also in precision, i.e. in the number of decimal places. Doubles are twice as precise as floats, which you probably deduced from its name.

Data type Range Precision
float +-3.4 * 10−38 to +-3.4 * 1038 7 digits
double +-1.7 * 10−308 to +-1.7 * 10308 15 digits
long double +-1.7 * 10−308 to +-1.7 * 10308 15 digits

Note: the long double type is sometimes the same as double or even bigger, however, it's the same in Visual Studio.

Beware, due to the fact that decimal numbers are stored in your computer in a binary system, there is some precision loss. Although the deviation is almost negligible, if you're programming, e.g. a financial system, it could lead to slight deviations.

When we need to assign a value to a float variable in the source code, we have to use the f suffix:

double d = 2.72;
float f = 3.14f;

As the decimal separator in a source code, we use dots, regardless of our OS' regional settings.

Other built-in data types

Let's look at the other data types that C++ offers:

Data type Range Size/Precision Meaning
char U+00 to U+ff (0 to 255 or -128 to 127) 8 bites character
wchar_t U+0000 to U+ffff (0 to 65 535) 16 bites "wide" character
bool true or false (1 or 0) 8 bites logical value

Char represents one character, unlike strings, which represents an entire string of chars. Characters are declared with apostrophes in C++:

char c = 'A';

A single, non-array, char actually belongs in the list of whole-number variables. It contains a numeric character code, but it seemed more logical for me to put it here.


Variables of the bool type can only contain two values: true or false. We'll use them when we get to conditions. In a variable of the bool type, we can store either true/false or a logical expression. Let's make a simple example:

bool b = false;
bool expression = (15 > 5);
cout << b << endl;
cout << expression << endl;

The program output:

Console application

We write expressions in parentheses. Notice that the expression applies, i.e. is equal to true since 15 really is more than 5. Going from expressions to conditions isn't a far stretch, but we'll go into them in the next lesson.

There's still a lot to go over and lots of other data types that we haven't covered. Regardless, there is a time for everything. In the next lesson, Conditions (branching) in the C++ language, we'll introduce conditions and then loops. Then, we'll have enough knowledge to create interesting programs :)



Article has been written for you by David Capka
Do you like this article?
No one has rated this quite yet, be the first one!
The author is a programmer, who likes web technologies and being the lead/chief article writer at ICT.social. He shares his knowledge with the community and is always looking to improve. He believes that anyone can do what they set their mind to.
Unicorn College The author learned IT at the Unicorn College - a prestigious college providing education on IT and economics.
Activities (4)




To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

No one has commented yet - be the first!