C Operators can be classified in to the following categories :
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Increment and Decrement Operators
- Conditional Operators
- Bitwise Operators
- Special Operators
1. Arthimetic Operators :C provides all the basic arithmetic operators.
The operators +,-, * and / all work the same way as they do in other languages. These can operator on any built in data type allowed in C.
Integer division operation truncates any fractional part(or) it gives the quotient of an integer division.
The modulo division operation products the remainder of an integer division.
All arithmetic operators are binary operators, because they act on two operands, The operators *, /, % will be given the highest priority over + and -.
When equal priority operators are there in an expression, the operators will be evaluated from left to right i.e they follow left to right associativity
sum = b + c; sub = b - c; mul = b * c;
div = b / c; rem = b % d;
Example : arithmetic expression : a = b / c * d;
when b = 10, c = 5, d = 3, the value of a = 6.
Here b, c, d are variables and are known as operands.
|+||Addition||1 + 2 = 3|
|-||Substraction||3 - 2 = 1|
|*||Multiplication||2 * 2 = 4|
|/||Division||3 / 2 = 1|
|%||ModuloDivision||10 % 3 = 1|
- The modulo division operator % cannot be used on floating point data.
- C does not have an operator for exponentiation.
- ANSI C supports unary plus
Arithmetic operations are of the following three types :
- Integer arithmetic
- Real arithmetic
- Mixed mode arithmetic
When both the operands in a single arithmetic expression such as a + b are integers, then the expression is called integer expression, and the operation is called integer arithmetic. Integer arithmetic always yields an integer value. The largest integer value depends on the machine.
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 (reminder of division)
During integer division, if both the operands are of the same sign, the result is truncated towards zero. If one of them is negative, the direction of truncate is implementation dependent. That is 6 / 7 = 0 and -6 / -7 = 0
But -6 / 7 may be zero or -1 (Machine dependent)
Similarly, during modulo division, the sign of the result is always the sign of the first operand( the dividend). That is
-14 % -3 = -2 and 14 % -3 = 2
Real arithmetic :
An arithmetic operation involving only real operands is called Real arithmetic. A real operand may assume values either in decimal or exponential notation. Since floating point values are rounded to the number of significant digits permissible, the final value is an approximation of the correct result.
If x, y and z are float, then we will have :
y = 1.0 / 3.0 = 0.333333
z = -2.0 / 3.0 = -0.666667
The operator % can not be used with real operands.
The expression 30.0 % 4.0 is invalid expression.
Mixed mode arithmetic :
When one of the operands is real and the other is integer, the expression is called a mixed mode arithmetic expression.
It either operand is of the real type, then only real operation is preformed and the result is always a real number. Thus