Arithmetic Operators in C



The arithmetic operators in C are certain special symbols, predefined to perform the arithmetic operations. We are familiar with the basic arithmetic operations addition, subtraction, multiplication and division. C is a computational language; hence these operators are essential in performing a computerised process. In addition to the above operations assigned to the four symbols +, -, *, and / respectively; C has another arithmetic operator called modulo operator and the % symbol is assigned with this operation.

The following table lists the arithmetic operators in C −

Operator Description
+ Adds two operands.
Subtracts second operand from the first.
* Multiplies both operands.
/ Divides numerator by denominator.
% Modulus Operator and remainder of after an integer division.
++ Increment operator increases the integer value by one.
-- Decrement operator decreases the integer value by one.

The ++ and – operators are also listed in the above table. We shall learn about increment and decrement operators in a separate chapter.

Example

The following example demonstrates the use of the above arithmetic operators.

#include <stdio.h>

int main() {
   int op1 = 10;
   int op2 = 3;

   printf("operand1: %d operand2: %d\n", op1, op2);
   printf("addition of op1 and op2: %d\n", op1+op2);
   printf("subtraction of op2 from op1: %d\n", op1-op2);
   printf("multiplication of op1 and op2: %d\n", op1*op2);
   printf("division of op1 by op2: %d\n", op1/op2);

   return 0;
}

Output

operand1: 10 operand2: 3
addition of op1 and op2: 13
subtraction of op2 from op1: 7
multiplication of op1 and op2: 30
division of op1 by op2: 3

Type casting

The first three results are as expected, but the result of division is not. You expect 10/3 to be a fractional number (3.333333). Is it because we used %d format specifier to print the division? If we change the last line.

printf("division of op1 by op2: %f\n", op1/op2);

It results in the result as 0.000000, which is even more surprising. The reason for this behaviour is that in C, the division of an integer with integer always returns integer. To obtain floating-point division, at least one operand must be in float, or you need to use typecast operator to change one of the integer operands to float.

Change the last printf() statement as below −

printf("division of op1 by op2: %f\n", (float)op1/op2);

It now shows the correct division −

division of op1 by op2: 3.333333

The result of arithmetic operations with at least one float (or double) operand is always float.

Example

#include <stdio.h>

int main() {
   int op1 = 10;
   float op2 = 2.5;

   printf("operand1: %d operand2: %f\n", op1, op2);
   printf("addition of op1 and op2: %f\n", op1+op2);
   printf("subtraction of op2 from op1: %f\n", op1-op2);
   printf("multiplication of op1 and op2: %f\n", op1*op2);
   printf("division of op1 by op2: %f\n", op1/op2);

   return 0;
}

Output

operand1: 10 operand2: 2.500000
addition of op1 and op2: 12.500000
subtraction of op2 from op1: 7.500000
multiplication of op1 and op2: 25.000000
division of op1 by op2: 4.000000

If you use %d format specifier for a floating-point expression, it will always result in 0.

Arithmetic with char type

In C, char data type is a subset of int type. Hence, we can perform the arithmetic operations with char operands.

Example

#include <stdio.h>

int main() {
   char op1 = 'F';
   int op2 = 3;

   printf("operand1: %c operand2: %d\n", op1, op2);
   printf("addition of op1 and op2: %d\n", op1+op2);
   printf("subtraction of op2 from op1: %d\n", op1-op2);
   printf("multiplication of op1 and op2: %d\n", op1*op2);
   printf("division of op1 by op2: %d\n", op1/op2);

   return 0;
}

Output

operand1: F operand2: 3
addition of op1 and op2: 73
subtraction of op2 from op1: 67
multiplication of op1 and op2: 210
division of op1 by op2: 23

Since a char data type is a subset of int, the %c format specifier returns the ASCII character associated to an integer returned by %d specifier. If any arithmetic operation between two char operands results in an integer beyond the range of char, the %c specifier displays blank.

Modulo operator

The % operator in C has been defined as a modulo operator and returns the remainder of a division operation. As a result, if x%y results in z, then x/y+z is equal to x.

Example

#include <stdio.h>

int main() {
   int op1 = 10;
   int op2 = 3;

   printf("operand1: %d operand2: %d\n", op1, op2);
   printf("modulo of op1 and op2: %d\n", op1%op2);

   return 0;
}

Output

operand1: 10 operand2: 3
modulo of op1 and op2: 1

The modulo operator needs both the operands of int type. If not, the compiler gives type mismatch error −

float op1 = 10;
int op2 = 3;
printf("modulo of op1 and op2: %d\n", op1%op2);

Output

error: invalid operands to binary % (have 'float' and 'int')

It does allow char operands for modulo operations though.

Negation operator

Although the increment and decrement operators represented by ++ and – symbols, they are unary operators. They have been covered in a separate chapter. The – symbol, representing subtraction operator, also acts a unary negation operator.

Example

#include <stdio.h>

int main() {
   int op1 = 5;
   int op2 = -op1;

   printf("operand1: %d operand2: %d\n", op1, op2);

   return 0;
}

Output

operand1: 5 operand2: -5

In the above example, the – symbol returns the negative value of op1 and assigns the same to op2.

c_operators.htm
Advertisements