'C' provides all the basic arithmetic operators. They are listed in table. The operators +, -, * and / all work the same way as they do in other language.
Operator: +, Meaning: Addition or unary plus.
Operator: -, Meaning: Subtraction or unary minus.
Operator: *, Meaning: Multiplication.
Operator: /, Meaning: Division.
Operator: %, Meaning: Modulo division.
Examples of use of arithmetic operators are:
a-b a+b
a*b a/b,
a%b -a*b.
Here a and b are variables and are known as operands. The modulo division operator % cannot be used on floating point data.
Integer Arithmetic:
Examples, if a and b are integers then for a = 14 and b = 4 we have the following results.
a-b = 10
a+b = 18
a*b = 56
a/b = 3 (Decimal part truncated).
a%b = 2 (Remainder of division)
Real Arithmetic:
An arithmetic operation involving only real operands is called real arithmetic. If x, y and 2 are floats, then we will have
x = 6.0/7.0 = 0.857143.
y = 1.0/3/0 = 0.333333
Programming in ANSIC:
z = -2.0/3.0 = -0.666667
The operator % cannot be used with real operands.
Mixed - mode Arithmetic
When one of the operand is integer in the expression. If either operands is of the real type, then only the real operation is performed and the result is always a real number. Thus,
15/10.0 = 1.5
Where as
15/10 = 1
...
Operator: +, Meaning: Addition or unary plus.
Operator: -, Meaning: Subtraction or unary minus.
Operator: *, Meaning: Multiplication.
Operator: /, Meaning: Division.
Operator: %, Meaning: Modulo division.
Examples of use of arithmetic operators are:
a-b a+b
a*b a/b,
a%b -a*b.
Here a and b are variables and are known as operands. The modulo division operator % cannot be used on floating point data.
Integer Arithmetic:
Examples, if a and b are integers then for a = 14 and b = 4 we have the following results.
a-b = 10
a+b = 18
a*b = 56
a/b = 3 (Decimal part truncated).
a%b = 2 (Remainder of division)
Real Arithmetic:
An arithmetic operation involving only real operands is called real arithmetic. If x, y and 2 are floats, then we will have
x = 6.0/7.0 = 0.857143.
y = 1.0/3/0 = 0.333333
Programming in ANSIC:
z = -2.0/3.0 = -0.666667
The operator % cannot be used with real operands.
Mixed - mode Arithmetic
When one of the operand is integer in the expression. If either operands is of the real type, then only the real operation is performed and the result is always a real number. Thus,
15/10.0 = 1.5
Where as
15/10 = 1
...