A small book for those beginners in C++ or even in programming who didn’t understand C++ data types. This is meant to be a patch to other books, that cover the basics of C++. Data types can be a little tricky sometimes, and difficult to understand, this book explains how data types work, and also shows you how to use them by giving examples.
Introduction
Book’s purpose
This is a short book for those beginners in C++ or even in programming who didn’t understand data types in C++. This book will only cover the C++ own types and will not teach you how you can create your own data types. There will be examples of how you should use data types and how you shouldn’t. This book is divided in separate parts, this is part 1. It will teach you C++ integers. Float will be covered in part 2 of this book.
What you should know
This book is for those who know the basics of C++. You can still learn data types from this book without knowing the basics of C++, but I recommend you to read a complete book first, and then, when you reach the data types lesson in that book, and after you read it, you read this short book too, to be sure you completely understand data types.
What you should have
I’m using Microsoft Windows XP as the operating system, and that means that this book applies only to Windows users, because data types differ in other operating systems like UNIX or Mac OS.
You must have a compiler of course, and in this book, we use Visual C++ 6 Enterprise Edition. It makes a difference which compiler you are using, because data types often differ from one compiler to the other.
What data types are
Data types
Many programming languages use different data types. This data types can hold different values. There are some data types that can hold small values, and use less memory, and there are data types that can hold bigger values, but use much more memory.
Adequate data type for adequate value
There is no reason for you to store a small value in a big data type, because the space that it occupies in the computer memory is wasted.
Let’s suppose you have a five characters word, like ‘aloha’, and the smallest data types you have can hold one character, five, or ten. Which should you use? Of course, if you can’t use the data type that holds one character because it has room to store only the first character, ‘a’. You can use the data type that holds ten characters, and the situation would look like this:
Not very clever. You’re wasting five additional characters. The five characters that will remain empty will be reserved, because you declared 10 characters to be used. Therefore, the remaining 5 empty spaces cannot be used by another instance of a data type. It’s reserved for this data type instance only. On the other side, you can expand the word later, if you wish, to a maximum of 10 characters. For example, you can write ‘alohaaaa!’ and fill all the empty spaces.
However, if you know this value doesn’t change why should you use the ten characters data type and not the five characters one?
You save memory space and at the same time, your program gains performance.
This was only an example to demonstrate you why data types are important. This is not a real-life example from C++. In the next lessons, we will work with more real-life examples.
Now you can read ‘Understanding C++ data types II’