Unlike Python and Matlab, where the language takes care of how data is stored in variables, in C++, it is up to you to tell the compiler what the type of each
is. As already discussed, there are some built-in types that are available. We will discuss these here.
The simplest type is bool, which has a value of either true or false, and these values can be used in the conditions of either conditional statements or condition-controled looping statements.
bool variable_name{ true }; bool another_variable_name{ false };
Boolean-valued variables can be assigned:
We approximate real numbers in the computer with floating-point numbers, and there are two common types: double and float. The term double comes from double-precision floating-point numbers, so as you may guess, the type double has twice as much precision as the type float.
The type double is approximately equivalent to storing 16 significant decimal digits of precision, while the type float is qpproximately equivalent to storing 7 significant decimal digits of precision. For almost all engineering applications, you must use double. The only real application of float are graphics.
Different declarations and initializations include:
double variable_name{ 325.92853 }; double another_variable_name{ -0.002358053 }; double a_third_variable_name{ 3.928e13 };
where the last means $3.928 \times 10^{13}$.
A double precision can be assigned:
Integer types are left to the end as they are much more subtle. Integer types tend to occupy 1, 2, 4 or 8 bytes.
For example, short and unsigned short usually occupy two bytes, and this allows variables declared as such to store $2^{16} = 65536$ different values, so
If you add two numbers that go beyond these ranges, the types will overflow.
Next, int and unsigned int usually occupy four bytes, and this allows variables declared as such to store $2^{32}$ (approximately four billion) different values, so
Again, if you go beyond this range, the types overflow.
Next, long and unsigned long usually occupy eight bytes (but in the Microsoft Visual Studio compiler, they only ocupy four, making them equivalent to int and unsigned int, respectively), and this allows varaibles declared as such to store $2^{64}$ (approximately 18.5 quntillion) different values, so
There are, however, more standardized types. If you include the C standard integer library, you have access to a number of other types:
#include <cstdint>
You can now use int8_t and uint8_t (the number indicating 8 bits, the u indicating unsigned) to allow to to store integers using only one byte (so values between -128 and 127 and 0 and 255, respectively); as well as int16_t and uint16_t; int32_t and uint32_t; and int64_t and uint64_t for two, four and eight bytes, both signed and unsigned, respectively.
You can read more about this library at C++.com.
The char type allows you to store one ASCII character. This includes all they keys you see on a standard English keyboard together with a few special non-printable control characters.
To code a character, you must place the character in single quotes:
char ch_1{'a'}; char ch_2{'@'}; char ch_3{'|'};
To allow you to code non-printable control characters, a special character is used to indicate that the next character should be interpreted differently. Such a character is said to be an escape character, and the two characters together form an escape sequence:
char ch_4{'\\'}; // A single backslash char ch_4{'\''}; // A single quote char ch_5{'\t'}; // A Tab char ch_7{'\n'}; // A new line char ch_6{'\b'}; // The ringing of a bell
as well as others. You will note that the backslash, being the character used to indicate that the next character should be interpreted differently must be indicated with two backslashes. Because a single quote is used to delimit the character, it too must be escaped.
You will likely not use characters in most of your projects.
Literal strings are a sequence of characters, possibly none, surrounded by double quotes. As you may guess, escape sequences are needed here, too, specifically, you need to escape the double quote character. This piece of code prints the given string:
std::cout << "Hello world! \\\t\\\"\t" << std::endl;
If you used other C-like programming languages, you may have leanred that the character '\n' is an end-of-line character, meaning that it moves the cursor to the next line when printing. This is actually not standardized, so it is better to use the generic end-of-line object std::endl to print.