c operators

 
Operators in 'c' language

Operator is  a symbol which operates on operand. 

Example 3+2. Here 3 and 2 is called operand and + is called operator .

 On the basis of operand operators are three types.

  • Unary operator
  • Binary operator
  • Ternary operator
Unary operator
    The operator which has only a single operand is called unary operator.
Example:-++a,a++.

Binary operator
    The operator which has two operand is called binary operator.
Example:-a+b

Ternary operator
    The operator which has more than two operand is called ternary operator.
Example:-a?a:b;

Types of operator:
  1. Arithmetic operator
  2. Modular division operator
  3. Increment/decrement operator
  4. Relational operator
  5. Logical operator
  6. Bitwise operator
  7. Assignment operator
  8. Conditional operator
1. Arithmetic operator
    These are binary operators having two operands. 
These operators are addition(+), subtraction(-), multiplication(*),division[(/)(quotient)].

#include<stdio.h>
main( )
{
int a=10,b=5,add=0,sub=0,mul=0,div=0;
add=a+b;
sub=a-b;
mul=a*b;
div=a/b;
printf("%d is the addition",add);
printf("\n%d is the subtraction",sub);
printf("\n%d is the multipliaction",mul);
printf("\n%d is the division",div);
return 0;
}


2.Modular division operator
    It is a binary operator having two operand the operation of this operator is to show remainder in a division. The symbol of this operator is %.

#include<stdio.h>
main( )
{
int a=10,b=5,rem=0;
rem=a%b;
printf("\n%d is the remainder",rem);
return 0;
}
ans:0

3.Increment/decrement operator
    It is a unary operator having a single operand. The operation of this operator is to increment the value of the variable by 1 and it is classified in to two categories.
    A.prefix increment operator:++a.
        In this case the operator is present before the operand the operation mechanism is to first increment the value of the variable by 1 after that assign the value.

#include<stdio.h>
main( )
{
int a=10,b=0;
b=++a;
printf("the value of a is %d\n",a);
printf("the value of b is %d",b);
}

the value of a is 11
the value of b is 11

    B.postfix increment operator:a++
        In this case the operator is present after the operand the operation mechanism is to first assign the value of the variable after that increment by 1.

#include<stdio.h>
main( )
{
int a=10,b=0;
b=a++;
printf("the value of a is %d\n",a);
printf("the value of b is %d",b);
}
the value of a is 11
the value of b is 10
 
4.Relational operator
    These are also binary operators which has two operands the operation of this operator is to check the relationship between two operands if the relationship is matched then it returns true otherwise it returns false.
    These operators are >,<,>=,<=,==,!=;

#include<stdio.h>
main( )
{
int a=10,b=52,c=0;
c=a>b;
printf("%d",c);
}
Ans:0
5.Logical operator.
    It is also both binary and unary operator. The operation of this operator is based on logical data true or false. These operators are

               *    && Logical operator

            *      || Logical operator

            *       ! Logical not operator

Logical  and operator:&&

            This is a binary operator .If both the operands are true then it return true or 1 otherwise it return false or 0.

                     

 

A

B

A&&B

1

1

1

1

0

0

0

1

0

0

0

                               0

               Truth table for logical and operator

#include<stdio.h>

main( )

{

int a=10,b=52,c=0;

c=a&&b;

printf("%d",c);

}


Ans=1

 Logical OR operator: | |

            It is also a binary operator that means having two operand, if both the operand are 0 or false then it return false or 0 otherwise it return 1 or true.

A

B

A||B

1

1

1

1

0

1

0

1

1

0

0

0


                                Truth table for logical or operator
#include<stdio.h>
main( )
{
int a=10,b=52,c=0;
c=a&&b;
printf("%d",c);
}
Ans=1

 LOGICAL NOT operator: !

              It is a unary operator that means it has a single operand .The operation of this operator is convert true to false and vice versa.

A

!A

1

0

0

1

#include<stdio.h>

main( )

{

int a=9,b=10,c=0;

c=!(a>b);

printf("%d",c);

}

Ans=1


6.BITWISE OPERATOR

           These are binary operator one which is an  unary opeartor . The operation of this operator is to manipulation on bitwise register data therefore here the operand must be of integer or character type. These  operators are:

         ~   Bitwise compliment operator

       &   Bitwise and operator

       |      Bitwise or operator

       ^       Bitwise xor operator

        <<     Bitwise left shift operator

       >>      Bitwise right operator

 ~ (Bitwise compliment operator):

     It is an unary operator . The operation of this operator is to convert the binary register data 1 to 0 and 0 to1.

      main ()

      {     int a=5,b;

              b=~a;

       printf (“%d”,b);  outputis-6

     }

 

 

 

 

 

1

0

1

1

1

1

1

1

0

1

0

(Bitwise And operator)&

       This is a binary operator .The operation of this opertor is to manipulate on binary register data of two operand bit by bit .If bit value of the both operands are one then it returns 1 otherwise it returns 0.

A

B

A&B

1

1

1

1

0

0

0

1

0

0

0

0

#include<stdio.h>  

main()

      {

            int a=5,b=3,c=0;

           c=a&b;

            printf("%d",c);   

     }

ans=1

A

1

0

1

B

0

1

1

A&B

0

0

1

(Bitwise OR operator)|

      It is a binary operator . it is also operates on binary register data bit by bit . if the both the operands bit value is 0 then returns 0.other wise it is return 1.

A

B

A|B

1

1

1

1

0

1

0

1

1

0

0

0

    main ()

    {

              int a=5,b=3,c;

               c=a|b;

              printf(“d%”,c);7

    }

 

A

1

0

1

B

0

1

1

A|B

1

1

1

     ^(Bitwise Xor operator)

       Itr is a binary operator . it is also operates on binary register data bit by bit.if the both the operands bit value is 0or 1it returns 1. Otherwise it is return 0.

A

B

A^B

1

1

0

1

0

1

0

1

1

0

0

0

   main ()

    {

        int a=5,b=3,c;

         c=a^b’;

         printf(“d%”,c);   6

     }

  

A

1

0

1

B

0

1

1

A^B

1

1

0

   << (BITWISE LEFT SHIFT OPERATOR)

    This is also a binary operator. The operation of this operator is shift binary register data from right to left. In this process every time one bit of data is shifted at a time.

      Syntax:-data << No of bit shifted

    main ()

   {

       int a = 5, b;

       b= a << 2;

        print (“%d”, b);

     }            

        formula (Data * 2no of bit shifted)

    >> (BITWISE RIGHT SHIFT OPERATOR)

    This is also a binary operator. The operator of this operator is shift binary register data from left to right . In this process every time one bit of data is shifted at a time .

      Syntax :-  data << No of bit shifted

      main ()

      {

            int a= 20 ,b=0;

            Print (“%d”,b);

        }      formula ( Data /2no of bit shifted)


7.ASSIGNMENT OPERATOR

               These are binary operator .The operation of this operator is to assign the right side value on the left side variable, after a specified operation .These operators are:-

                      =: equal,   -=: minus equal,       *=: multiplication equal

                      +=: plus equal      /=: division equal     %=: M division equal

   N: B Write some common example program on this Assignment operator.

       main ()

     {               int a=5, b=10;

                     a +=b     /* here a and b add with each other Then addition value on a */

                     Print (“%d %d”, a, b);            output is a =15, b=10

    }

      main ()

    {             int a=5, b=10;

                   a-=;     /*here a and b subtract with each other Then assign the subtraction value on a*/

                   Print (“%d %d”, a, b);         output is a=-5, b=10

     } etc.


8.conditional operator.
    It is a ternary operator having three operand. The first operand is called condition and represented by ? and the second two operands are called statements. The first statement represented by colon (:) and the second statement represented by (;).
    a?a:b;
    #include<stdio.h>  

main()
{
int a=10;
a%2==0?printf("even"):printf("odd");
}

Ans:even


    

No comments: