Types of Operator

Arithmetic Operator

1.      ‘C’ language provides some basic operators which are used to perform arithmetic operations such as addition,   subtraction, multiplication and division. These operators are called arithmetic operator.

2.      The Arithmetic operator wortks on integer , float and character types of value.

3.      The following are the arithmetic operator in ‘C’ language:-

SNo
Operator
Meaning
Example
1.
+
Addition or unary plus
int i=20+2;
2.
-
Subtraction or unary minus
int i=20-2;
3.
*
Multiplication
int i=20*2;
4.
/
Division
int i=20/2;
5.
%
Remainder
int i=20%2;
6.
-
Unary Minus
int i= -20

Case1:-When the both operand in a single arithmetic expression are integer, the expression is called integer expression and the operation is called integer arithmetic. The result of Integer Arithmetic is always integer.
Num=10/2;                  à        5         

Case2:-When the both operand in a single arithmetic expression are float, the expression is called real expression and the operation is called real arithmetic. The result of real Arithmetic is always real
Num=10.25/2.5;          à        4.1

Case3:-When the one of the operand in a single arithmetic expression are float, the expression is called mixed mode expression and the operation is called mix mode arithmetic. The result of real mix mode arithmetic is always real.
Num=12.6/2;               à        6.3

Case 4:-The unary minus operator multiply the operand value with -1.Thus this operator is used to change the sign of operand in ‘C’ language.
int Num= -10;
Result= - Num;             à        10

Case 5:-The % operator is called modules/remainder operator in ‘C’ language.This operator divide the operand1 by the operand2 and returns the remainder of division.
Num=13%2;                à        1

Note:-This operator works with only integer type values. If we use this operator on decimal operand it displays an error message.
Num=13.56%2;           à        Error

The sign of the result is always the sign of first operand.
Num= -16/3    à         -1                    Num= 16/-3  à           1
Num= -16/-3  à         -1


The following program show the use of arithmetic operators in ‘C' Language.
#include <stdio.h>
#include <conio.h>
void main()
{
      int Num1=10 , Num2=2;
      clrscr();

      printf("Addition is %i\n", Num1 + Num2);
      printf("Subtraction is %i\n", Num1- Num2);
      printf("Multiplication is %i\n", Num1*Num2);
      printf("Division is %i\n", Num1/Num2);
      printf("Remainder is %i", Num1%Num2);
      getch();
}

WAP to input five subjects marks and calculate the percentage.
#include<stdio.h>
#include<conio.h>
void main ()
{

      int s1,s2,s3,s4,OM,TM=400,p;
      clrscr();
      printf("Enter four subject marks :");
      scanf("%i%i%i%i",&s1,&s2,&s3,&s4);
      OM=s1+s2+s3+s4;
      p=OM*100/TM;
      printf("Percentage is :%i",p);
      getch ();
}

WAP to input two numbers and interchange them with the help of third variable.
#include<stdio.h>
#include<conio.h>
void main()
{
      int N1, N2,Temp;
      clrscr();
      printf("Enter two numbers");
      scanf("%i %i",&N1,&N2);
      Temp=N1;
      N1=N2;
      N2=Temp;

      printf("After Interchange");
      printf("N1=%i\n N2=%i",N1,N2)
      getch();
}




WAP to input a character and display its ASCII value.
#include<stdio.h>
#include<conio.h>
void main()
{     char ch;
      clrscr();
      printf("Enter a character :");
      scanf("%c",&ch);
      printf("Ascii Code is %i",ch);
      getch();
}

WAP to input a upper case character and convert it into lower case.
#include<stdio.h>
#include<conio.h>
void main()
{
      char ch;
      clrscr();
      printf ("Enter a upper case char");
      scanf  ("%c",&ch);
      ch=ch+32;
      printf ("Lower case char is %c",ch);
      getch();
 }

WAP to input a number and reverse it.
#include<stdio.h>
#include<conio.h>
void main()
{
      int last_digit, number, next_digit, rev_num;
      clrscr();

      printf ("Enter the five-digit number:");
      scanf("%d", &number);

      last_digit = number - ((number / 10) * 10);
      rev_num = last_digit; /* 5 */

      next_digit = (number / 10) - ((number / 100) * 10);
      rev_num = (rev_num * 10) + next_digit; /*54*/

      next_digit = (number / 100) - ((number / 1000) * 10);
      rev_num = (rev_num * 10) + next_digit; /*543*/

      next_digit = (number / 1000) - ((number / 10000) * 10);
      rev_num = (rev_num * 10) + next_digit; /*5432*/

      next_digit = (number / 10000) - ((number / 100000) * 10);
      rev_num = (rev_num * 10) + next_digit;

      printf ("The Reversed Number is: %d",rev_num);
      getch ();
}

WAP to input a three digit number and print the sum of each digit.
#include<conio.h>
#include<stdio.h>
void main()
{
    int d1,d2,d3,Sum;
    int Num;
    printf("Enter a three digit number");
    scanf("%i",&num);

    d1=(Num%10);
    d2=(Num%100)/10;
    d3=(Num%1000)/100;
   
    Sum=d1+d2+d3;

    printf("\n The sum of the digits is %i",Sum);
}



Relational Operator

1.      ‘C’ language provides some basic operators which are used to compare two or more values and make the decision based on the result of comparesion.These operator are called relational operator in ‘C’ language.

2.      The result of relational operator is always boolean values true or false. In ‘C’ language false is represented by zero and true is represented by one (any non zero value).

3.      Generally Relational operators are used with decision/conditional statements such as if, switch. The following are the relational operator in ‘C’ language:-

SNo
Operator
Meaning
Example
1.
< 
Is less then
Int I=10<20;
2.
<=
Less then equal to
Int I=10<=10;
3.
> 
Is greater then
Int I=10>20;
4.
>=
Is greater then equal to
Int I=10>=20;
5.
= =
Is equal to
Int I=10 = = 20
6.
!=
Is Not equal to
Int I=10!=20;

Ex: -     10<20                          à        True                             Ex: -     20<10                          à        false

Ex: -     int Num=10<20           à        1                                  Ex: -     int Num=20<10           à        0

The following program show the use of relational operators in ‘C' Language.
#include <stdio.h>
#include <conio.h>
void main()
{
int Num1=10 , Num2=20;
clrscr();
printf("Number1 > Number2 %i\n", Num1 > Num2);
printf("Number1 < Number2 %i\n", Num1< Num2);
printf("Number1 == Number2 %i\n", Num1== Num2);

getch();
}




Logical Operator

C’ language provides some basic operators which are used to combines two or more conditions in a single expression and make the decision based on the result of compression.

The logical operator works on the Boolean value and return the boolean value. These operators are called Logical operator in ‘C’ language.

‘C’ language provides the following three logical operators. They are:
1.      && Logical AND Operator
2.      || Logicl OR Operator
3.      ! Logical Not Operator


Logical AND Operator [&&]
1.      It is called Logical AND operator in ‘C’ language. This operator is used to combines two or more conditions into a single expression.

2.      This operator returns true (non zero) value only if both conditions are true otherwise it returns false (zero).

3.      The Logical AND Operator check the first condition. If the first condition result is true it will check the second condition

4.      The Logical AND Operator check the first condition. If the first condition result is false it will not check the second condition

5.      The truth table of logical AND operator is given below: -

Condition 1
Condition 2
Operator
Result
TRUE
TRUE
&&
TRUE
TRUE
FALSE
&&
FALSE
FALSE
TRUE
&&
FALSE
FALSE
FALSE
&&
FALSE

Ex: - 2<1 && 3>1       à        False && True à        False 

The following program show the use of Logical AND  operators in ‘C' Language.
#include <stdio.h>
#include <conio.h>
void main()
{
int R;
clrscr();
R=10<20  &&  3>1;

printf("Result is %i\n", R);
getch();
}



Logical OR Operator [| |]
1.      It is called OR operator in ‘C’ language. This operator is used to combines two or more conditions into a single expression.

2.      This operator returns false only if both conditions are false otherwise it returns true.

3.      The Logical OR Operator checks the first condition. If the first condition result is true it will not check the second condition

4.      The Logical OR Operator checks the first condition. If the first condition result is false it will  check the second condition

5.      The truth table of logical AND operator is given below: -

Condition 1
Condition 2
Operator
Result
TRUE
TRUE
||
TRUE
TRUE
FALSE
||
TRUE
FALSE
TRUE
||
TRUE
FALSE
FALSE
||
FALSE

Ex: - 2<1 || 3>1           à        False && True à        True 

The following program show the use of Logical OR  operators in ‘C' Language.
#include <stdio.h>
#include <conio.h>
void main()
{
int R;
clrscr();
R=10<20  ||  3<1;

printf("Result is %i\n", R);
getch();
}


iii]. !: - It is called NOT operator in ‘C’ language. This operator is used to change the relational expression result from true to false and false to true

!(TRUE)            à        FALSE                          
!( FALSE)          à        TRUE  

The following program show the use of Logical NOT operators in ‘C' Language.
#include <stdio.h>
#include <conio.h>
void main()
{
int R;
clrscr();
R=!(10<20);

printf("Result is %i\n", R);
getch();
}



Assignment Operator

‘C’ language provides some basic operators which are used to store some value or the result of expression into a variable. These operators are called assignment operators. The following are the assignment operator in ‘C’ language:-

SNo
Operator
Meaning
1.
=
Used to store a value into a variable
2.
+=
Add a specific value into variable current value
3.
- =
Subtract a specific value into variable current value
4.
*=
Multiply a specific value into variable current value
5.
/ =
Divide a specific value into variable current value
6.
%=
Calculate remainder of current variable and store in it



The following program show the use of assignment operators in ‘C' Language.

#include<stdio.h>
#include<conio.h>
void main()
{
           int n1=10;
           clrscr();

           printf("%i",n1+=2);
           printf("%i",n1-=2);
           printf("%i",n1*=2);
           printf("%i",n1/=2);
           printf("%i",n1%=2);

           getch();
}



Increment/Decrement Operator

1.      ‘C’ language provides some special operators which are used to increase or decrease the value of given variable by one. These operators are called Increment/Decrement operators.

2.      These operators works on a single value at a time therefore they are called Unary operators. They are generally used with loop constructs/statements.

Increment Operator
1.      The ++ Operator is called as the Increment Operator in C Language. This operator increases the value of variable by one.

2.      The Increment operator can be used before/after the operand. If the increment operator is used before the operand it is called pre increment operator. If the increment operator is used after the operand it is called post increment operator.
++ i;                       Pre increment
i++;                        Post Increment

3.      The pre increment operator first increase the value of operand by one then assign new value  to the given variable.
Int Num=100;
R=++Num;             Num=101                     R=101

4.      The post increment operator first assign the value of operand to the given variable then increase the value of a operand.
Int Num=100;
R=Num++;             R=100                          Num=101

The following program show the use of Pre Increment operators in ‘C' Language.
#include<stdio.h>
#include<conio.h>
void main()
{
int Num=10;
clrscr();
++Num;
printf(“%i”,Num);
getch();
}

The following program show the use of post Increment operators in ‘C' Language.
#include<stdio.h>
#include<conio.h>
void main()
{
int Num=10;
clrscr();
Num++;
printf(“%i”,Num);
getch();
}
Decrement Operator
1.      The - - Operator is called as the Decrement Operator in C Language. This operator decreases the value of variable by one.

2.      The Decrement operator can be used before/after the operand. If the Decrement operator is used before the operand it is called pre decrement operator. If the increment operator is used after the operand it is called post decrement operator.
++ i;                                   Pre decrement
i++;                                    Post Decrement

3.      The pre decrement operator first decrease the value of operand by one then assign new value to the given variable.
                                                  i.      Int Num=100;
                                                ii.      R=++Num;             Num=101                     R=101

4.      The post increment operator first assign the value of operand to the given variable then decrease the value of an operand.
                                                  i.      Int Num=100;
                                                ii.      R=Num++;             R=100                          Num=101


 The following program show the use of Pre Decrement operators in ‘C' Language.
#include<stdio.h>
#include<conio.h>
void main()
{
int Num=10;
clrscr();
--Num;
printf(“%i”,Num);
getch();
}

The following program show the use of Post Decrement operators in ‘C' Language.
#include<stdio.h>
#include<conio.h>
void main()
{
int Num=10;
clrscr();
Num--;
printf(“%i”,Num);
getch();
}

Conditional Operator

‘C’ language provides a special operator which works on three operand at a time. This operator is called conditional operator. The general form of conditional operator is
                        exp1    ?          exp2    :           exp3

The conditional (? :) operator first execute the exp1.If exp1 return boolean value true (non zero) it execute exp2.If exp1 return false (0) it execute exp3.
                        Num1=500;
                        Num2=100;
                        Result= Num1> Num2 ? Num1: Num2;                       à        Result =500    

Note: -Because this operator works on three operand it is called ternary operator. Only one of the expression is execute at a time either exp2 or exp3.

WAP to input a number and convert it into absolute value.
#include<stdio.h>
#include<conio.h>
void main ()
{
      int Num;;
      clrscr ();

      printf("Enter a Number :");
      scanf ("%i",&Num);

      Num=Num>0 ? Num : -Num ;
      printf("Absolute value is %i", Num);

      getch();
}

WAP to input a number and check it whether it is even or odd.
#include<stdio.h>
#include<conio.h>
void main ()
{
      int Num;
      clrscr ();

      printf("Enter a Number");
      scanf ("%i",&Num);

      Num%2==0 ? printf("Even Number") :printf("Odd Number");
      getch();
}




WAP to input two numbers and find the largest between them.
#include<stdio.h>
#include<conio.h>
void main ()
{
      int Num1, Num2;
      clrscr ();

      printf("Enter Two Numbers :");
      scanf ("%d %d",&Num1, &Num2);

      Num1>Num2 ? printf("%d is Largest",Num1) :printf("%d is Largest",Num2);
      getch();
}

WAP to input a number and check it whether it is positive or negative.
#include<stdio.h>
#include<conio.h>
void main()
{
      int num;
      clrscr();
      printf("Enter Number");
      scanf("%i",&num);
      num>0?printf("positive"):printf("negative");
      getch();
}







7]. Bitwise Operator

1.      Arithmetic, Relational and logical operator perform operation on the variable value while the bitwise operator performs the operation on the bits value of a variable.

2.      The bitwise operator first convert the variable value into binary form internally, performs the operation, convert the binary result back to integer and then display the result to the user.

3.      These operators are used to test the bits, shifts the bits from left to right/right to left and calculate the complement of bits.

These operators do not work on the decimal values (float, double).The bitwise operator can be classified into following categories:-
i). Bitwise Logical Operator               
ii). Bitwise Shift Operator                  
iii). Bitwise Compliment Operator

0
0
0
0
1
0
0
0
List Significant Bit               Most Significant Bit   



i). Bitwise Logical Operator: -They are binary operators. There are three types of bitwise logical operator. They are:-
1.      Bitwise AND
2.      Bitwise OR
3.      Bitwise XOR

a).Bitwise AND (&):-It is binary operator which is  represented by &.This operator returns binary value one if both bits are one otherwise it return zero.

BIT 1
BIT 2
Operator
RESULT
1
1
&
1
1
0
&
0
0
1
&
0
0
0
&
0

Ex

128
64
32
16
8
4
2
0/1
Num1=10
0
0
0
0
1
0
0
0
Num2=15
0
0
0
0
1
1
1
1







&

Result 10
0
0
0
0
1
0
0
0


The following program show the use of bitwise logical AND operator in ‘C' Language.
#include <stdio.h>
#include <conio.h>
void main()
{
      int Num1=10 , Num2=15,R;
      clrscr();

      R=Num1 & Num2;

      printf("Bitwise AND is %i\n", R);

      getch();
}



b). Bitwise OR (|): - This operator returns binary value zero if both bits are one otherwise it returns one.           

BIT 1
BIT 2
Operator
RESULT
1
1
|
1
1
0
|
1
0
1
|
1
0
0
|
0
           
Ex

128
64
32
16
8
4
2
0/1
Num1=10
0
0
0
0
1
0
0
0
Num2=15
0
0
0
0
1
1
1
1







|

Result 15
0
0
0
0
1
1
1
1
           

The following program show the use of bitwise logical OR operator in ‘C' Language.
#include <stdio.h>
#include <conio.h>
void main()
{
      int Num1=10 , Num2=15,R;
      clrscr();

      R=Num1 | Num2;

      printf("Bitwise OR is %i\n", R);

      getch();
}


c).Bitwise XOR (^):-This operator returns binary value one if any bits are one otherwise it return zero.

BIT 1
BIT 2
Operator
RESULT
1
1
^
0
1
0
^
1
0
1
^
1
0
0
^
0

Ex:

128
64
32
16
8
4
2
0/1
Num1=10
0
0
0
0
1
0
1
0
Num2=15
0
0
0
0
1
1
1
1







^

Result  5
0
0
0
0
0
1
0
1

The following program show the use of bitwise logical XOR operator in ‘C' Language.
#include <stdio.h>
#include <conio.h>
void main()
{
      int Num1=10 , Num2=15,R;
      clrscr();

      R=Num1 ^ Num2;

      printf("Bitwise XOR is %i\n", R);

      getch();
}



ii). Bitwise Shift Operator: -They are used to shift bits from left to right or right to left and fill the empty bits with zero.’C’ language provides three shift operators. They are: -
1.      Left Shift (<<)
2.      Right Shift (>>)

a).Left Shift (<<): -This operator moves the all bits to the left and fill the empty bits with zero.


128
64
32
16
8
4
2
0/1
Num1=10
0
0
0
0
1
0
1
0
Num2=Num1<<2
0
0
1
0
1
0
- -
- -









Result  40
0
0
1
0
1
0
0
0


The following program show the use of bitwise left Shift operator in ‘C' Language.
#include <stdio.h>
#include <conio.h>
void main()
{
      int Num1=10,R;
      clrscr();

      R=Num1 <<1;

      printf("Bitwise Left Shift is %i\n", R);

      getch();
}



b).Right Shift (>>): - This operator moves the all bits to the right and fills the empty bits with zero.   
                        

128
64
32
16
8
4
2
0/1
Num1=10
0
0
0
0
1
0
1
0
Num2=Num1>>2
- -
- -
0
0
0
0
1
0









Result  2
0
0
0
0
0
0
1
0

The following program show the use of bitwise Right Shift operator in ‘C' Language.
#include <stdio.h>
#include <conio.h>
void main()
{
      int Num1=10,R;
      clrscr();

      R=Num1 >>1;

      printf("Bitwise Right Shift is %i\n", R);

      getch();
}


iii). Bitwise Compliment Operator (~): -It is a unary operator which invert (reverse) all the bits of an operand. It change all one bits to zero and all zero bits to one.

           
128
64
32
16
8
4
2
0/1
Num1=10
0
0
0
0
1
0
1
0
Num2= ~Num1
1
1
1
1
0
1
0
1









Result  -11
1
1
1
1
0
1
0
1

The following program show the use of bitwise left Shift operator in ‘C' Language.
#include <stdio.h>
#include <conio.h>
void main()
{
      int Num1=10,R;
      clrscr();

      R=~Num1;

      printf("Bitwise Compliment is %i\n", R);

      getch();
}



WAP which interchange the values of two variables with the help of bitwise XOR operator.
#include <stdio.h>
#include<conio.h>
void main()
{

  int Num1=10;
  int Num2=20;
  clrscr();
  printf("Before Interchange Num1= %i and Num2= %i", Num1,Num2);

  Num1 ^= Num2;
  Num2 ^= Num1;
  Num1 ^= Num2;

  printf("\nAfter Interchange Num1= %i and Num2= %i", Num1,Num2);
  getch();
}



8]. Special Operator

The following are the some special operator in ‘C’ language:
1.      sizeof : - This operator return an integer value that determine the number of bytes occupied by a variable, constant or data type qualifier such as int, char etc.
int Num=sizeof (float);                  à        4

2.      &: -It is called addressof operator. It returns the address of the specified variable.

3.      * : -It is called valueof operator. It returns the value of a pointer variable.

4.      . :-It is called memberof operator.It is used to access structure members.

5.      -> :- It is called memberof operator.It is used  with link list to access a node.

6.      , :-It is called coma operator.It is used to link related expression/value together. It executed the expression from left to right and the value of rightmost expression is the result of comma-linked expression
Ex: - Result= (Num1=10, Num2=15, Num1+Num2);                     à25

It first assigns value 10 to Num1 variable then assign 15 to Num2. Finally add num1 and Num2 value and store the result into Result variable.

This operator is also used with for loop.
Ex: -for (i=1; i<=10; printf (“Hello”), i++)              à        print Hello ten Times

Note:-If an expression contains all the unary and binary operators then unary operator execute first then binary operators.

The following program show the use of sizeof operator in ‘C' Language.
#include <stdio.h>
#include <conio.h>
void main()
{
      int i=10;
      float f=100.65;
      char c='X';
      double d=1000.5678;
      clrscr();

      printf("Integer Memory :%i\n",sizeof(i));
      printf("Float Memory :%i\n",sizeof(f));
      printf("Character Memory :%i\n",sizeof(c));
      printf("Double Memory :%i\n",sizeof(d));

      getch();
}

Comments