# 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 languages has the following operators:

- Arithmetic Operators.
- Relational Operators.
- Logical Operators.
- Conditional Operators.
- Assignment Operators.
- Bitwise Operators.
- sizeof Operator.
- Arithmetic Operators

The following table contains all arithmetic operators.

Operator | Name |
---|---|

+ | addition |

- | subtraction |

* | multiplication |

/ | division |

% | modulus operator |

As you may already know how the first four operator works, but you may not have come across the `%`

operator. `%`

operator is known as called modulus operator or modular division operator. Modulus operator (`%`

) is used to calculate 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 `%`

operator on `float`

and `double`

types.

All these operators listed on the table above are binary operators because they require two operands to operate. But `+`

and `-`

operators have their unary versions too, For e.g:

`+x`

or `-y`

In this case `+`

operator has no effect on the number `x`

but `-`

sign changes the sign of the operand `y`

.

## Integer arithmetic

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 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 `11/4 = 2.75`

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

```
#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:**

```
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 floating point result. Let's take two variables `a`

and `b`

, such that `a = 11.2`

and `b = 4.5`

. The Following table shows 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.

`#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:**

```
a + b = 11.300000
a - b = 7.100000
a * b = 19.320000
a / b = 4.380952
```

**Note:** `%`

modulus operator can be used with floating point constant.

## 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 `11/4`

. To get the correct answer we have to make one of the operands a floating point number.

For example `11/4.0`

or `11.0/4`

both will give the correct result i.e `2.75`

.

```
#include<stdio.h>
int main()
{
printf("%f\n", 11/4.0);
// Signal to operating system everything works fine
return 0;
}
```

**Expected Output:**

```
2.750000
1212
```