Monday, July 8, 2013

Mathematical Operators in C

Mathematical Operators are those operators through which we perform some Arithmetic operations in C. These Operators are broadly classified into two types

  1. Unary Operators
  2. Binary Operators
Both the Operators are responsible for performing the Arithmetic Operators, but they are differed based on the number of Operands they use.

Unary Operator:

Unary Operators are those operators in which only one operand comes into play i.e., they perform operations on a single operand or a variable. The following table gives the list of unary operators in C.

Operator
Symbol
Description
Examples
Increment
++
It increments the operand by one
X++, ++X
Decrement
--
It decrements the operand by one
Y--, --Y

In the above table, we have two kinds of operators, Increment and Decrement. In Increment Operator, we add one to the operand i.e., if we write x++, it means that x=x+1. Similarly for the decrement. 

Again these Increment and Decrement are divided into two types, Pre-Increment, Post-Increment and Pre-Decrement and Post-Decrement. x++ is post increment and ++x is pre-increment. There is a lot difference in between these two operators even though they mean the same as x=x+1.

If we say, x++ in a program, we first use the x and then increment the value of x.
If we say, ++x in a program, it means we first increment x and then use the value of x.

Similarly for the decrement Operators also. To know more about these Operators, click here.

Binary Operators:

Binary Operators are the operators performed on two operands. These include most commonly used mathematical operators. The following table gives you a description of the most commonly used operators.

Operator
Symbol
Description
Examples
Addition
+
Adds the two operands
x+y
Subtraction
-
Subtracts the second operand from the first
x-y
Multiplication
*
Multiplies the two operands
x*y
Division
/
Divides the first operand with second operand and gives quotient
x / y
Modulus
%
Gives the remainder when first operand is divided with second operand
x % y
All of these operators are almost familiar to people except the modulo operator. We will be using these operators throughout the C programming. So be familiar with the operators and their usage.