Ads

Operators

- An operator is a special character or a special symbol which is used to perform a specific operation on operands. And here in C programming language, operands means the constants stored in memory.

EX: i = 5 + 3; In this statement 5 and 3 are called operands and = and + are called operators. After the calculation result is stored in a memory location named i.

- Operators are classified into three types, based on the number of operands they operate on.
  • Unary Operator
  • Binary operator
  • Ternary operator
NOTE: It’s not that important that you understand the examples given in this category of operators, we will discuss them deeply in forthcoming topics.


UNARY OPERATORS:

- Unary operators are the operators which operate on single operand.
- Some unary operators change the value of the operand in the memory after each and every operation.
- Like b++, ++b, --b, b--(where b is a variable and     ++ --   are operators)

Program:

#include<stdio.h> /* Header file*/

int main() /* start of main function*/
{
  int a, b = 5;  /* First lets initialize memory location a and b */
  a = ++b;  /* operation*/
 printf("Value stored in a is %d",a); /* Printing the result stored in a*/

 return 0;

} /* End of main function*/




Output:
Value stored in a is 6


Some more examples
  •    a = ++b; /*Here b is incremented and the result is stored in a*/
  •    a = b++;
  •    a = --b;
  •    a = b--;

 

BINARY OPERATORS:

- Binary operators are the operators which operate on two operands.
- Binary operators mostly don’t change the values in memory of respective operands they are operating on.
- Examples: a+b, a-b, a*b, a/b, a%b etc.

Program:

#include<stdio.h>    /* Header file*/

int main()     /* start of main function*/
{
 int a=4, b = 5,c; /* First lets initialize a, b and c */
 c = a+b;    /* operation*/
 printf("Value stored in c is %d",c); /* Print the result stored in c*/

 return 0;
}      /* End of main function*/

Output:
Value stored in c is 9


TERNARY OPERATORS:

- Ternary operators are the operators which operate on three operands.
- Example: 5<3 ? 5 is small : 5 is big.

Program:


#include<stdio.h> /* Header file*/
int main()    /* start of main function*/
{
  5<3 ? printf("5 is small") : printf("5 is big"); /* operation*/
 return 0;
}     /* End of main function*/


Output:
                5 is big

Operator Classification based on operation:

- Above we discussed about classification of operators based on number of operands they operate on.
- Right now let us discuss about classification of operators based upon the type of operation they perform.
  • Operators are divided into
    • Arithmetic operators
    • Assignment Operator
    • Compound Assignment
    • Comparison /Relational 
    • Logical Operators
    • Conditional
    • Increment or Decrement
    • Bitwise Operators
    • Other operators(Sizeof(), Comma, Address)




No comments:

Post a Comment