# C++ Data-types and Operators

September 11, 2018

In this chapter we are going to explore the different C++ data types. C++ supports different data types such as integers and decimals (also known as floating-point values). We’ll also see variables briefly in this tutorial (and in more detail later on). Variables are used to store values.

# Boilerplate C++ code

// This is the "boilerplate" code for C++.
// All the C++ code you write in this course will have this layout.
#include <iostream> // We will always need the iostream library for input-output
// More #include statements here for adding any other libraries we need
using namespace std; // We will always need namespace std since cout is inside the std namespace
// main function - where the execution of program begins
int main()
{
// actual code here
// ...
return 0; // main always end with "return 0;"
}

The code for almost all the examples in this lesson and further lessons will have same format. If we do not provide the full code, remember that the snippets we provide go in the ’// actual code here’ part. We also recommend typing out the code in full (including the boilerplate) a few times, just to get some practice (copy pasting is bad for learning, type things out yourself).

# Integer data-type and variables

The integer data-type holds integer values such as 5, 42, -17, etc. To define the integer data type, use the int keyword.

int a = 42;

Above, a is a variable of type int (for integer). A good analogy for the variable is a box - where the name of the variable as the label on the box, and the value of the variable as what’s stored inside it. Currently, the variable a stores the value 42.

Try out the following code:

int a = 42;
cout << a << endl;
// Outputs 42

# Floating-point values (float data-type)

A floating point type variable can hold a real numbers such as 420.0, -3.33, 0.03325, etc. To define the floating-point data type, use the float keyword.

float b = 4.21;
cout << b << endl;
// Outputs 4.21

In general, declaring a variable of a particular data-type has the following format:

<data_type> <variable_name> = <value>;

# Simple math with arithmetic operators

Now, we’ll see some basic math that C++ can do. C++ supports the following arithmetic operators.

Try out the following code:

int x = 40 + 60;
cout << x << endl;
// Outputs 100

Similarly, we can do subtraction, multiplication, division, etc.

#include <iostream>
using namespace std;
int main()
{
cout << 40 + 60 << endl; // Outputs 100
cout << 60 - 40 << endl; // Outputs 20
cout << 60 * 40 << endl; // Outputs 2400
cout << 60 / 40 << endl; // Outputs 1 (not 1.5)!
return 0;
}

As you can see, addition, subtraction and multiplication work as expected. But division is strange. The result is 1 instead of 1.5. This is because an integer (example, 2, 10, -7) is a different data-type in C++ than floating-point values (example, 2.8, 3.14, -5.1). When you divide two integers, C++ returns an integer as the result.

If instead you want floating-point values in the answer, then you have to use floating-point values in the expression you provide to C++. For example:

cout << 14.0 / 5.0 << endl; // Outputs 2.8
cout << 7.2 / 2.5  << endl; // Outputs 2.88
cout << 7.0 / 3    << endl; // Outputs 2.33333

Notice that you don’t necessarily have to use the floating-point values for both the values. If you use floating-point value for any one of them, that works. This is because if C++ is asked to divide an an integer by a float (or a float by an integer), in first converts the integer to a float, and then performs the division. When we divide an integer by an integer on the other hand, C++ doesn’t convert anything to float.

Lastly, we have the modulus operator (%), which returns the remainder after an integer division.

int x = 25 % 7;
cout << x << endl;
// Outputs 4 (remainder when 25 is divided by 7)

# Expressions

Each of the lines shown previously is called an expression. Expressions can be much longer too. For example, try this:

cout << (5 + 2) * (7 - 1) / 3;
// Output 14

In C++ (and other programming languages as well), all expressions evaluate to a single value. You saw this previously for some simple expressions such as 40 + 60 which evaluates to 100.

Here’s a step-by-step breakdown of how the above expression is evaluated in C++.

(5 + 2) * (7 - 1) / 3
=> (7) * (7 - 1) / 3
=> 7 * (7 - 1) / 3
=> 7 * (6) / 3
=> 7 * 6 / 3
=> 42 / 3
=> 14

C++ does the above behind the scenes, and tells us what the final result of evaluating the expression is.

Another expression that we typed earlier requires some explanation, when we performed division with one float value and one int value.

7.0 / 3
=> 7.0 / 3.0
=> 2.33333

That is, when C++ performs division with one float and one int, it first converts the int to a float.

# Operator precedence

Operator precedence or BODMAS, is a rule of solving an expression with higher preference first, which affects how an expression is evaluated. Certain operators take higher precedence over others; for example, the multiplication operator has higher precedence over the addition operator. For example:

int x = 5 + 2 * 2;
cout << x;
// Outputs 9

The program above evaluates 2 * 2 first, and then adds the result to 5.

int x = (5 + 2) * 2;
cout << x;
// Outputs 14

Using brackets changes operator precedence. If we have an expression with brackets inside an expression which is also inside brackets, then first the inner expression is solved first, and then outer expression is solved.

If none of the expressions are in brackets (also known as parentheses), then multiplicative (multiplication, division, modulus) operators will be evaluated before additive (addition, subtraction) operators.

# Converting between float and int

Converting from float to int or from int to float works slightly differently in C++. This is called type-casting. The syntax for doing type-casting is different from what you’ve seen so far.

// Convert float to int
float f = 4.2;
cout << (int) f << endl; // outputs 4
// Convert int to float
int x = 5;
int y = 2;
// Convert int x to float, then divide by y
cout << ((float) x) / y << endl; // outputs 2.5
cout << x / y << endl; // outputs 2 (integer division)

There’s no straight-forward reason why C++ chose to have such quirky syntax for different things. In most programming languages, both printing (cout) and type-casting is all done using functions (like to_string()).

# Other Data Types

To conclude this tutorial, we’d like to mention a couple of other data types in C++. You’ll see them in more detail later in the course.

## Characters

Character is a data type that holds one character such as 'A', '4', or '#'. A character is enclosed between single quotes, and the corresponding keyword is char. For example:

char test = 'S';

## Booleans

Boolean variables only have two possible values: true and false. To declare a boolean variable, we use the keyword bool.

bool online = false;
bool logged_in = true;

If a boolean is converted to an integer, true becomes 1 and false becomes 0. If an integer is converted to a boolean, 0 becomes false and any other non-zero value becomes true.

# Summary

OK, enough of numbers, and rest of the data-types. So far you’ve learned about:

• numbers, characters and booleans – in C++ numbers are used for math, characters for a single character, and booleans for true/false values.
• operators – like + and *, combine values to produce a new one