Core C++

Lesson 4 : Basic Data Types

In this C++ tutorial, you will learn about four of the main data types in the language. The purpose of this lesson is to teach the data types that are most common and essential to the material that we will cover in the upcoming lessons. The four data types covered in this lesson are the int, double, char, and bool.

We introduce the int type in lesson 2 and gave more examples in lesson 3. Basically, int type variables can hold integers. This means that they can hold numbers like –2, 0, 1, or basically anything without a fractional part.

We introduced the double type in lesson 3. Unlike int type variables, a double can hold basically any real number, including ones with fractional parts.

Incidentally, double is short for double precision since this type has double the precision of the previous floating-point representation.




In this lesson, we introduce two totally new data types: chars and bools. The name char is short for character and this type is most often used to represent keyboard characters like "g", "&", or "?". However, char type variables can also hold "non-visible" keyboard characters like "tab and "enter."

Notice that we used single quote marks around the characters for the char type. This is C++ syntax. It is important to note that '4' is not equal to 4 and you must always use the single quote marks to denote the character as oppose to the value. The ' ' and ' ' represent the "tab" and "enter" keys, respectively.

Lastly, we have the bool type. A bool type variable can only take on two values: true and false. Typically, a bool is used to hold the result of some set of logical operators.

In C++, true and false are keywords with a specific meaning that is recognized by the compiler.




Since we have already covered variable initialization for ints and doubles, we will now go over a few examples of bools and chars. The syntax for these two cases is a little different, since we are not dealing with numbers directly.

Here is a program that handles the initialization for several char variables and outputs them to the console window. The output looks like this:

Notice that the "endline" and "tab" are initialized with a "\" before the letters. Also, all of the chars are in single quotes, except the last one. However, when we output them, we see only the characters. This is a little odd.

A char is actually a value between –128 and 127 and each keystroke corresponds to a value, but we will explain that more at a later time. The last char shows that the value 49 corresponds to '1'. Lastly, we point out that we use a "c" to indicate the char type in Hungarian notation.




Finally, this program uses a few bools. Mostly, this code is to get you familiar with the syntax of C++.

The words "true" and "false" are keywords in C++. As the output shows, they correspond to the values "1" and "0." However, it is best to always use the keywords to be clear.

 

© 2007–2024 XoaX.net LLC. All rights reserved.