Operators in C Languages

Operators

C support a rich set of built in operators. An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. Operators are used in the programs to manipulate data and variables.

C Operators can be classified in to the following categories :

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Assignment Operators
  5. Increment and Decrement Operators
  6. Conditional Operators
  7. Bitwise Operators
  8. Special Operators

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

Example

int sum = b + c;
int sub = b - c;
int mul = b * c;
int div = b / c;
int 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. The symbols *, / are known as operators (Arithmetic operators).

Operator Meaning Examples
+ Addition 1 + 2 = 3
- Substraction 3 - 2 = 1
* Multiplication 2 * 2 = 4
/ Division 3 / 2 = 1
% ModuloDivision 10 % 3 = 1

Note

  1. The modulo division operator % cannot be used on floating point data.
  2. C does not have an operator for exponentiation.
  3. ANSI C supports unary plus

Arithmetic operations are of the following three types :

  1. Integer arithmetic
  2. Real arithmetic
  3. Mixed mode arithmetic

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

Example

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

 -14 % 3 = -2 
-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 :

 x = 6.0 / 7.0 = 0.857143 
 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

 15 / 10.0 = 1.5 where as 15 / 10 =  
Next : Relational Operator in C Language

Post a Comment

0 Comments