Arithmetic Operators
Learn about the arithmetic operators in C++ and how to use them.
Operators
Operators are symbolic representations of conceptual ideas that we can use to perform various operations in programming. There are four types of operators:
- Arithmetic operators
- Binary operators
- Unary operators
- Logical operators
- Assignment operators
- Comparison operators
In this lesson, we’ll learn about the arithmetic operators in C++.
Arithmetic operators
Arithmetic operators can be of two types – binary and unary.
Binary arithmetic operators
As the name suggests, binary arithmetic operators require two operands.
Binary arithmetic operators, shown in the image above, are used to perform operations on numbers. The numbers can be of different types, such as integers, floats, or others.
To perform operations, we have two operands described in the image below:
Binary Operator
Symbol | Operation | Example |
| Addition | 13 + 5 replaced by 18 |
| Subtraction | 13 - 5 replaced by 8 |
| Multiplication | 13 * 5 replaced by 65 |
| Division | 13 / 5 replaced by 2 |
| Modulus | 13 % 5 replaced by 3 |
Operator precedence
If there are multiple operators in a single expression, the operations are evaluated based on the operator precedence. So, the operators with higher precedence have their operations evaluated first.
The *
, /
, and %
operators have the same precedence, and the +
, -
operators have the same precedence.
If an expression has operators of the same precedence, they are evaluated based on their associativity. For example, +
, -
and *
, /
follow left to right associativity in C++.
Let’s see an example:
3 + 5 - 8
Since both +
and -
operators have the same precedence, the one on the left will be evaluated first. So, 3 + 5
(equal to 8) will be evaluated, and then 8 - 8
will be evaluated.
Let’s see another example:
int x = 6 + 20 * 4;
Here, the multiplication operator, *
, is of higher level precedence than the addition operator +
. So, 20 * 4
will be evaluated first.
This would be equivalent to:
int x = 6 + (20 * 4);
If, we want 6 + 20
to be evaluated first, we’ll have to place them within parentheses, like this:
int x = (6 + 20) * 4;
In the previous example, 3 + 5 - 8
, the order of operations doesn’t change the end result. But in some cases, it would. To understand associativity, let’s see one such example.
In the expression, 10 / 2 * 6
, the division operator, /
, and the multiplication operator, *
, have the same precedence. Because the associativity of these operators is left to right, the division will be done first. Where 10/2
is equal to 5
, so the expression is equivalent to 5 * 6
, which evaluates to 30
.
If the order were changed, the result would be 10/12
. This knowledge is necessary so that we know when to use brackets to enforce the order that the program needs.
Run the code below to see the output:
#include <iostream>using namespace std;int main() {int result;// 5*6 = 30result = 10 / 2 * 6;cout << "10 / 2 * 6 = " << result;return 0;}
Solve the quiz below to see if you can get the answers right.
If int a = 10, b = -20
, then what will be the answer to the following expression?
c = a + 2 * b
-240
-30
-20
As mentioned above, operators can be used with operands of different data types in an expression. And the answers may not be as straightforward as you might think.
Let’s look at a few examples below:
While some results of the expressions above may be very predictable, we might find that some of the expressions have unpredictable answers.
There are several examples of implicit casting in the illustration above.
The result of the operator depends on the bigger of the two operand types.
For example, char
+ int
would be int
, unless cast into a char
.
For example, in the first slide, when adding an int
and a char
and storing the result inside an int
, we add the integer value and the ASCII value of the character (the result is an int
). Like in the expression 30 + 'A'
, we get 95
by adding the ASCII value of A
(which is 65
) with 30
.
In the example below, when adding int
and a char
and storing the result inside a char
, we get a char
. This is one example of implicit casting.
We’ve added a similar example in the code editor below. Run the program below to see for yourself:
#include <iostream>using namespace std;int main(){// initializing variable 'a'char a = 'A';// adding 2 to char 'a' and storing in 'b' of char typechar b = a + 2;// printing bcout << b << endl;// adding 2 to char 'a' and storing in 'c' of int typeint c = a + 2;// printing ccout << c;return 0;}
Let’s look at an example where we divide two integer numbers and then a float value with an integer.
Look at the code below and run it to see the difference:
#include <iostream>using namespace std;int main(){// initializing an int variableint num1 = 9;// initializing a float valuefloat num2 = 9.0;// dividing two integer variablescout << "Dividing two integer variables: " << num1/5 << endl;// dividing a float variable with an integer variablescout << "Dividing a float with an integer variable: " << num2/5 << endl;return 0;}
As we can see, the result of dividing two int
values is an int
value, whereas the result of dividing a float
value with an int
value is a float
value.
Unary operators
Unary operators are used with one operand. Unary operators also operate on integers and other data types like binary operators except for bool
.
Unary operators are of two types – postfix (x++
and x--
) and prefix (++x
and --x
).
Unary operators follow right-to-left associativity.
See the table below to understand how increment and decrement unary operators work.
Unary Operator
Symbol | Meaning | Example | Example |
| Increment (increase the value of variable by 1) | y = ++x; The pre-increment operator first increments x by 1 and then stores the incremented value in y. | y = x++; The post-increment operator first stores the value of x in y and then increments x by 1. |
| Decrement (decrease the value of variable by 1) | y = --x; The pre-decrement operator first decrements x by 1 and then stores the decremented value in y. | y = x--; The post-decrement operator first stores the value of x in y and then decrements x by 1. |
Working of ++
unary operator
The animation below shows how the pre-increment unary operator works.
Initially, we had x = 10
and y = 20
.
int y = ++x;
The variable x
is first incremented and then that incremented value of x
(x = 11
) is stored in y
(y = 11
).
Initially, we had x = 10
and y = 20
.
int y = x++;
The variable y
first stores the value of x
(x = 10
and so y = 10
) and after that the value of x
is incremented (x = 11
).
Working of --
unary operator
Initially, we had x = 10
and y = 20
.
int y = --x;
The variable x
is first decremented and then that decremented value of x
(x = 9
) is stored in y
(y = 9
).
Initially, we had x = 10
and y = 20
.
int y = x--;
The variable y
first stores the value of x
(x = 10
and so y = 10
) and after that the value of x
is decremented (x = 9
).
Now let’s solve the quiz below to check our understanding of unary operators.
What is the answer to the following question?
int x = 10, y = 20;
int c = (x++) - (--y);
cout<<c<<endl;
x = 11
, y = 19
, c = -9
x = 11
, y = 19
, c = -8