Arithmetic Operators in C
In this tutorial, we will dive into different arithmetic operators available in the C programming language.
In this tutorial, we will dive into different arithmetic operators available in the C programming language.
We have all done a lot of calculations in mathematics throughout our careers. We know how to add, subtract, multiply, and divide numbers. The same can be done in the C programming language, as we may often need to introduce mathematical equations in our code to solve specific problems. As a result, arithmetic operators play a vital role in solving problems involving mathematical concepts.
Arithmetic operators are those that can be used to perform a variety of arithmetic operations, including but not limited to addition, subtraction, multiplication, and division.
Arithmetic operators can be divided into two categories:
The binary arithmetic operators operate on two operands. This is the reason why they are binary operators. There are a total of 5 binary arithmetic operators:
And, the unary arithmetic operators operate on a single operand. There are 4 unary arithmetic operators:
Let’s study them one by one.
The addition operator (+) is used to add two operands. The following program demonstrates the same:
#include <stdio.h>
int main()
{
int x = 10, y = 20;
printf("%d", x + y);
return 0;
}
Output:
30
The subtraction operator (-) is used to subtract the second operand from the first operand. The following program demonstrates the same:
#include <stdio.h>
int main()
{
int x = 10, y = 20;
printf("%d", x - y);
return 0;
}
Output:
-10
The multiplication operator (*) is used to multiply two operands. The following program demonstrates the same:
#include <stdio.h>
int main()
{
int x = 10, y = 20;
printf("%d", x * y);
return 0;
}
Output:
200
The division operator (/) is used to divide the first operand by the second operand. The following program demonstrates the same:
#include <stdio.h>
int main()
{
int x = 10, y = 20;
printf("%d", x / y);
return 0;
}
Output:
0
Note that the division operation resulted in an integer value because both the operands are integers. If at least one operand is float, then the result would also be float. Also, in order to print the floating-point result, the format specifier inside the printf should be replaced to %f as shown in the following program:
#include <stdio.h>
int main()
{
float x = 10; //float variable
int y = 20;
printf("%.2f", x / y); // %.2f is used to print the result up to two decimal places.
return 0;
}
Output:
0.50
The modulus operator (%) gives the remainder when the first operand is divided by the second operand.
Consider the following program:
#include <stdio.h>
int main()
{
int x = 10, y = 20;
printf("%d", x % y);
return 0;
}
Output:
10
The unary plus operator does not change the value of the operand. It simply returns the operand as it is.
Let’s look at an example program:
#include <stdio.h>
int main()
{
int x = -10;
printf("%d", +x);
return 0;
}
Output:
-10
In the above program, variable x has a negative value. Inside the printf function, unary plus operator is applied to x, but it will not change its sign. The value of x will be printed on the screen exactly as it is, without any modifications to its sign.
The unary minus operator changes the sign of its operand. The positive number becomes negative, and negative becomes negative.
Let’s look at an example program:
#include <stdio.h>
int main()
{
int x = -10;
int y = 20;
printf("%d %d", -x, -y);
return 0;
}
Output:
10 -20
We will delve into this topic in more detail later, as it is a complex one. Let’s ignore it for now.
Consider the following expression:
2 * 3 + 10
What should be the resultant value of the above expression? Is it 26 or 16?
If we perform multiplication before addition, i.e. (2 * 3) + 10 then the result is 16. If addition is performed before multiplication, i.e. 2 * (3 + 10), then the result is 26. Which one is correct?
Understand that an expression like above can have exactly one result. To avoid such ambiguity when we don’t know the order of evaluation, the precedence and associativity rules can be followed. These rules help us determine the order in which operations should be performed when evaluating an expression. These rules are similar to the rules we use in mathematics.
The following table shows the precedence (or priority) of arithmetic operators:
Priority | Operators |
---|---|
1st | Unary + and – |
2nd | *, /, % |
3rd | +, – |
4th | = |
As we continue, we will gradually build our precedence table and discuss other operators. The increment and decrement operators will also be included in the table.
Consider the following expression:
2 * 3 / 5
Both multiplication and division have the same precedence, so which operation will be performed first? These types of situations can be resolved using the rules of associativity. When an expression contains operators with the same precedence, then associativity comes into picture.
The following table shows the associativity of arithmetic operators:
Priority | Operators | Associativity |
---|---|---|
1st | Unary + and – | Right to left |
2nd | *, /, % | Left to right |
3rd | +, – | Left to right |
4th | = | Right to left |
So, if we follow the associativity rules, the expression 2 * 3 / 5
will be evaluated from left to right.
2 * 3 / 5
6 / 5
1
Consider one more expression:
x = y = 10
The above expression contains only assignment operators. The order of evaluation of the assignment operator is right to left. This means 10 is first assigned to y and then y is assigned to x.
Now, consider the following expression:
10 / 20 + 30 * 40
In the above expression, it does not matter whether 10 is first divided by 20 or 30 is first multiplied by 40. The result will remain the same. Hence, the compiler will decide the order evaluation. It may choose to perform multiplication before division.
Example 1: Solve the expression x = 2 + 3 / 3 - 5 + 8 * 9 - 10 / 3
Solution: the order of evaluation is as follows:
x = 2 + 3 / 3 - 5 + 8 * 9 - 10 / 3
x = 2 + 1 - 5 + 8 * 9 - 10 / 3 (3/3 = 1)
x = 2 + 1 - 5 + 72 - 10 / 3 (8 * 9 = 72)
x = 2 + 1 - 5 + 72 - 3 (10 / 3 = 3)
x = 3 - 5 + 72 - 3 (2 + 1 = 3)
x = -2 + 72 - 3 (3 - 5 = -2)
x = 70 - 3 (-2 + 72 = 70)
x = 67
Example 2: Solve the expression x = z = 2 % 8 / -2 + 3
Solution: the order of evaluation is as follows:
x = z = 2 % 8 / -2 + 3
x = z = 2 / -2 + 3 (2 % 8 = 2)
x = z = -1 + 3 (2 / -2 = -1)
x = z = 2 (-1 + 3 = 2)
x = 2, z = 2
Solve the following expressions:
p = q = r = 10 + 30 – 40 / 5 % 3
s = 3 + 4 – 5 / 2
z = 10 / 4 / 5 / 6
t = 2.2 + 4.4 / 2 – 3.0
x = a * a + b * b – 2 * a * b (assume that a = 3 and b = 5)
p = q = r = 10 + 30 - 40 / 5 % 3
p = q = r = 10 + 30 - 8 % 3 (40 / 5 = 8)
p = q = r = 10 + 30 - 2 (8 % 3 = 2)
p = q = r = 40 - 2 (10 + 30 = 40)
p = q = r = 38 (40 - 2 = 38)
s = 3 + 4 - 5 / 2
s = 3 + 4 - 2 (5 / 2 = 2)
s = 7 - 2 (3 + 4 = 7)
s = 5 (7 - 2 = 5)
z = 10 / 4 / 5 / 6
z = 2 / 5 / 6 (10 / 4 = 2)
z = 0 / 6 (2 / 5 = 0)
z = 0 (0 / 6 = 0)
t = 2.2 + 4.4 / 2 - 3.0
t = 2.2 + 2.2 - 3.0 (4.4 / 2 = 2.2)
t = 4.4 - 3.0 (2.2 + 2.2 = 4.4)
t = 1.4 (4.4 - 3.0 = 1.4)
x = a * a + b * b - 2 * a * b
x = 3 * 3 + 5 * 5 - 2 * 3 * 5
x = 9 + 5 * 5 - 2 * 3 * 5 (3 * 3 = 9)
x = 9 + 25 - 2 * 3 * 5 (5 * 5 = 25)
x = 9 + 25 - 30 (2 * 3 * 5 = 30)
x = 34 - 30 (9 + 25 = 34)
x = 4 (34 - 30 = 4)
By the way, the above equation is the formula of (a - b)^2 = a^2 + b^2 - 2ab
. You can directly apply the formula:
x = (a - b)^2
x = (3 - 5)^2
x = (-2)^2
x = 4
The answer is same 😉
Leave a comment