# Arithmetic Operators in C

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:

1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Conditional Operators
5. Assignment Operators
6. Bitwise Operators
7. sizeof Operator
8. 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:

Expected Output:

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.

Expected Output:

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`.

Expected Output:

### 3 thoughts on “Arithmetic Operators in C”

1. This blog is really awesome …..entire concept is explained in brief & accurate way…….

2. Very Good explanation..

3. concepts are explained in a neat and simple way.