Arithmetic Operators in C
Last updated on July 27, 2020
Operator: An operator specifies an operation on the data which yields a value.
Operand: Data item on which operator act is called operand.
Some operators need two operands while some need only one. C language provides the following operators:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Conditional Operators
- Assignment Operators
- Bitwise Operators
- sizeof Operator
- Arithmetic Operators
In this chapter, we are discussing Arithmetic Operators. The following table lists the arithmetic operators.
Operator | Name |
---|---|
+ |
addition |
- |
subtraction |
* |
multiplication |
/ |
division |
% |
modulus operator |
The first four operators work as usual, but you might not have come across the %
operator. The %
operator is known as the modulus operator or modular division operator. Modulus operator (%
) is used to calculate the remainder. For example 9 % 2
would produce 1
because when 9
is divided by 2
it leaves a remainder of 1
. The important thing to note that is that it works only with integers, you can't apply the %
operator on float
and double
types.
All the operators listed in the table above are binary operators because they require two operands to operate. But +
and -
operators have their unary versions too, For example:
+x
or -y
In this case, the +
operator has no effect on the number x
but, the -
operator changes the sign of the operand y
.
Integer arithmetic #
When both operands are integers then the result of the arithmetic operation between two integer operands yields an integer value. Let's take two variables a
and b
, such that a = 10
and b = 4
. The following table shows the result of arithmetic operations performed on a
and b
.
Expression | Result |
---|---|
a + b |
14 |
a - b |
6 |
a * b |
40 |
a / b |
2 |
a % b |
2 |
We know that 10/4 = 2.5
, but because both operands are integers, the decimal value is truncated. For division and modulus operator to work the second operand i.e b
must be non-zero, otherwise, the program will crash.
The following program demonstrates integer arithmetic in action:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include<stdio.h>
int main()
{
// Declare and initialize variable a and b
int a = 11, b = 4;
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
// because both operands are integer result will be an integer
printf("a %% b = %d\n", a % b);
// % operator returns the remainder of 11/4 i.e 3
// Signal to operating system everything works fine
return 0;
}
|
Expected Output:
1 2 3 4 5 | a + b = 15
a - b = 7
a * b = 44
a / b = 2
a % b = 3
|
Pay close attention to how %
character is used in the printf()
statement in line 17. We already know that %
character is used to specify conversion specification inside the control string. To print a single %
character in the console we have to write %
two times.
Floating Point Arithmetic #
An operation between two floating point operands always yields a floating point result. Let's take two variables a
and b
, such that a = 11.2
and b = 4.5
. The following table shows the result of arithmetic operations performed on a
and b
.
Expression | Result |
---|---|
a + b |
15.7 |
a - b |
6.700000 |
a * b |
50.400000 |
a / b |
2.488889 |
The following program demonstrates floating point arithmetic in action.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include<stdio.h>
int main()
{
// Declare and initialize variable a and b
double a = 9.2, b = 2.1;
printf("a + b = %f\n", a + b);
printf("a - b = %f\n", a - b);
printf("a * b = %f\n", a * b);
printf("a / b = %f\n", a / b);
// Signal to operating system everything works fine
return 0;
}
|
Expected Output:
1 2 3 4 | a + b = 11.300000
a - b = 7.100000
a * b = 19.320000
a / b = 4.380952
|
Note: The %
modulus operator can't be used with floating point constants.
Mixed mode arithmetic #
An operation between an integer and a floating point yields a floating point result. In this operation, the integral value is first converted to floating point value then the operation is performed. Let's take two variables a
and b
, such that a = 14
and b = 2.5
. The following table shows arithmetic operations performed on a
and b
.
Expression | Result |
---|---|
a + b |
16.500000 |
a - b |
12.500000 |
a * b |
35.000000 |
a / b |
5.600000 |
As we can see, when 14
is divided by 2.5
the fractional part is not lost because arithmetic operation between an int
and a double
yields a double
value. So we can use Mixed mode arithmetic to solve the problem we encountered while dividing 10/4
. To get the correct answer simply make one of the operands involved in the operation a floating point number. For example, 10/4.0
or 10.0/4
both will give the correct result i.e 2.5
.
1 2 3 4 5 6 7 8 9 10 | #include<stdio.h>
int main()
{
printf("%f\n", 10/4.0);
printf("%f\n", 10.0/4);
// Signal to operating system everything works fine
return 0;
}
|
Expected Output:
1 2 | 2.500000
2.500000
|
Load Comments