Now in order to make some advanced programs, we need to know about this basic thing called Operators. I have expained earlier about micro processor programming where we use statements like 2E 23,34 for instructing the processor to perform an add operation. But it is very low level, and it is difficult for us. So better approach which C give us is Operators.

In general terms, an operator is a function that specify specific actions on objects. Those objects which acts as an input to the operator is called an operand. Let us see the different types of operators used in C. The following are categorised based on the operation they perform.

i) Arithmetic Operators : These operators are used for performing arithmetic operations like Addition, Subtraction, Multiplication, Division, Modulus Operator.

While the first four operators can be used for floating point numbers also, modulo operators can be used only for Integers.

ii) Assignment Operators : If you look at the above code, you could see that we have used "=". This is called the assignment operator. What it does is, evaluates the expression on the right hand side and assigns the result of the evaluation to the operand in the left hand side. Hence the left hand operand cannot be an expression.

There are also some special assignment operator called Short hand operator.

This is pretty much useful while we need to store the result in one of the operands.

iii) Relational operators : These operators are used for comparing two operands. The result of the operation is either true(1 or non zero value) and false(0). It is used in conditional statements where we need to perform certain set of operation if a condition is true, or another set of operations if a condition is false.

This post is getting pretty big, so lets continue in my next post on operators :)

In general terms, an operator is a function that specify specific actions on objects. Those objects which acts as an input to the operator is called an operand. Let us see the different types of operators used in C. The following are categorised based on the operation they perform.

i) Arithmetic Operators : These operators are used for performing arithmetic operations like Addition, Subtraction, Multiplication, Division, Modulus Operator.

Code

iNum3 = iNum1 + iNum2; // Addition operation

iNum3 = iNum1 - iNum2; // Subtraction operation

iNum3 = iNum1 * iNum2; // Multiplication operation

iNum3 = iNum1 / iNum2; // Division operation

iNum3 = iNum1 % iNum2; // Modulo operation (Gives remainder of division operation - iNum1/iNum2)

While the first four operators can be used for floating point numbers also, modulo operators can be used only for Integers.

ii) Assignment Operators : If you look at the above code, you could see that we have used "=". This is called the assignment operator. What it does is, evaluates the expression on the right hand side and assigns the result of the evaluation to the operand in the left hand side. Hence the left hand operand cannot be an expression.

There are also some special assignment operator called Short hand operator.

Code

iNum1 += iNum2; // Similar to iNum1 = iNum1 + iNum2

iNum1 -= iNum2; // Similar to iNum1 = iNum1 - iNum2

iNum1 *= iNum2; // Similar to iNum1 = iNum1 * iNum2

iNum1 /= iNum2; // Similar to iNum1 = iNum1 / iNum2

iNum1 %= iNum2; // Similar to iNum1 = iNum1 % iNum2

This is pretty much useful while we need to store the result in one of the operands.

iii) Relational operators : These operators are used for comparing two operands. The result of the operation is either true(1 or non zero value) and false(0). It is used in conditional statements where we need to perform certain set of operation if a condition is true, or another set of operations if a condition is false.

Code

int iNum1=10;

int iNum2=20;

// Checks whether iNum1 is equal to iNum2

printf("%d\n",iNum1 == iNum2);

// Checks whether iNum1 is greater than iNum2

printf("%d\n",iNum1 > iNum2);

// Checks whether iNum1 is greater than or equal to iNum2

printf("%d\n",iNum1 >= iNum2);

// Checks whether iNum1 is lesser than iNum2

printf("%d\n",iNum1 < iNum2);

// Checks whether iNum1 is lesser than or equal to iNum2

printf("%d\n",iNum1 <= iNum2);

// Checks whether iNum1 is not equal to iNum2

printf("%d\n",iNum1 != iNum2);

Output

0

0

0

1

1

1

This post is getting pretty big, so lets continue in my next post on operators :)

## 0 comments:

## Post a Comment