Skip to the content of the web site.

Types

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

  • local variable,
  • function paramter, and
  • function return value

is. As already discussed, there are some built-in types that are available. We will discuss these here.


Boolean type

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:

  • one of the two Boolean true or false,
  • the result of a comparison operator (==, !=, <, <=, >= or >=),
  • the result of a logical operator (&& for logical AND, || for logical OR and ! for logical NOT.

Floating-point types

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:

  • a literal floating-point number like 3.1415, or
  • the result of an arithmetic expression that includes +, -, * or /. Note that there is no exponential operator in C++.

Integer types

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

  • short stores between -32768 and
  • 32767, and
  • unsigned short stores between 0 and
  • 65536.

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

  • int stores between -2147483648 and
  • 2147483647, and
  • unsigned int stores between 0 and
  • 4294967296.

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

  • long stores between -9223372036854775808 and
  • 9223372036854775807, and
  • unsigned long stores between 0 and
  • 18446744073709551615.

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.


Characters

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.


Strings

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.