Arithmetic operators


An operator is a symbol which helps the user to command the computer to do a certain mathematical or logical manipulations on data and variables. The value on which an operation is performed is called operand. C has a rich set of operators which can be classified as
1. Arithmetic operators 
2. Relational Operators 
3. Logical Operators 
4. Assignment Operators 
5. Increments and Decrement Operators 
6. Conditional Operators 
7. Bitwise Operators 
8. Special Operators

1. Arithmetic operators 
All the basic arithmetic operations can be carried out in C. All the operators have almost the same meaning as in other languages. Both unary and binary operations are available in C language. Unary operations operate on a singe operand, therefore the number 5 when operated by unary – will have the value –5.

Operator
Meaning
+
Addition or Unary Plus
-
Subtraction or Unary Minus
*
Multiplication
/
Division
%
Modulus Operator

Integer division gives coefficient part where as the modulus operator produces the remainder of the operands after division. 
Eg:-     14/3=4             14%3=2
The modulo division operator cannot be used on floating point data.
Eg:-     14/3=4.666667            14%3=invalid

Integer Arithmetic

When an arithmetic operation is performed on two whole numbers or integers than such an operation is called as integer arithmetic. It always gives an integer as the result. 
Eg:-     Let a = 31 and b = 4 
a + b = 35
a - b = 27
a * b = 124
a / b = 7
a % b = 3

An expression containing operator and integer operands  is known as “integer expression”.
During integer division, if both operands have same sign, the result is positive where as if one of them is negative, the result will be negative.
19  /  4  = 4
-19 /  4  = -4
19  / -4  = -4
-19 / -4 =  4
During modulo division, the sign of the result is always the sign of the first operand (dividend).
19  /  4  = 3
-19 /  4  = -3
19  / -4  = 3
-19 / -4 =  -3

Real or Floating point arithmetic

When an arithmetic operation is preformed on two real numbers or floating point numbers or fraction numbers, such an operation is called floating point arithmetic. The floating point results can be truncated according to the properties requirement. The remainder operator is not applicable for floating point arithmetic operands. 
Eg:-     Let a = 31.0 and b = 4.0
a + b = 35.000000
a - b = 27.000000
a * b = 124.000000
a / b = 7.750000

if  x and y are float numbers. Then
            x =  6.0/7.0 = 0.857143
            y = -1.0/3.0 = -0.333333

Mixed mode arithmetic

When one of the operand is real and other is an integer and if the arithmetic operation is carried out on these 2 operands then it is called as mixed mode arithmetic. If any one operand is of real type then the result will always be real thus 34/10.0 = 3.400000

No comments:

Post a Comment