Arithmetic Operators

There are 5 arithmetic operators in C language.

  • * (Multiplication), / (Divide) and % (Modulus)
  • +(addition) and – (Subtraction)

Operators *,/ and % are having same priority but higher priority than + and -. Operator + and – are having equal priority.






For example, in the following expression
x=3+4*5/2-7;

first we solve *, as it has higher priority than + and -. Whenever two or more arithmetic operators of the same priority comes in the expression, then we have resolve them in left to right manner. Since * comes in the left in comparison to / operator, it will be solved first.
Associativity rule is left to right for arithmetic operators, which means equal priority operators will be resolved in left to right manner.

Step 1: x=3+20/2-7;
Step 2: x=3+10-7;
Step 3: x=13-7;
Step 4: x=6;

behavior of operators
3+4 is 7
3-4 is -1
3*4 is 12
3/4 is 0

+,- and * operators behaves as expected but divide operator seems to be different.
Reason: Operation between two integer will give integer result only.
3 and 4 are integers, mathematically result should be 0.75 but in C language decimal point and subsequent digits are ignored and only integral part is considered. So the answer is 0.

More examples:
4/3 is 1
12/5 is 2
15/5 is 3
-15/2 is -7

Now observer following expressions

3.0/4 is 0.75
3/4.0 is 0.75
3.0/4.0 is 0.75

When at least one of the operand is real, the result will be real.

Modulus operator (%)
Modulus operator gives remainder as a result.

Following are few examples

5%2 is 1
17%5 is 2
10%2 is 0
21%10 is 1
3%4 is 3

Remember: In C language operands of modulus operator can’t be a real value. So 3.5%2 is an error.

Arithmetic Operators