operators in c

operators in c programming

operators are used in c programming to manipulate data and variables.

operators :
      Operator is a symbol which is use to perform a specified task. operator tells the compiler to perform arithmetic or logical operations.
      operator mainly divided into two categories.
1. binary operator
2. unary operator

1. binary operator :
      An operator which required minimum 2 operands to perform a certain task is known as a binary operator.

syntax :

operand 1 operator operand 2

example :

a+b
a-b
a>b

2. unary operator :
      An operator which required only single operand to perform a certain task is known as a unary operator.

syntax :

operator operand
or
operand operator

example :

++a
- -b
a++
b- -

types of operators :

1. arithmetic operators(+, -, *, /, %)
2. assignment operators(=, +=, -=, *=, /=, %=)
3. relational operators(==, !=, >, <, >=, <=)
4. logical operators(&&, ||, !)
5. increment and decrement operators(++, --)
6. conditional operator or ternary operators( ? ,  : )
7. bitwise operators(&, |, ^, ~, >>, <<)
8. special operators( sizeof () )

1. Arithmetic operators :
      This operators are use to perform algebraic operations between two or more operands. it is binary operators.
there are following operators are used in arithmetic operations
+, -, *, /, %

% operator returns remainder of the operand and it does not use in floating data type value.arithmetic operator in c

example :

      int a=10, b=20, c=3;

      a+b      30
      b%c      2
      c/3       1
      a*b       200
      c-10      -7

2. Assignment operators :
      This operator is used to assigning a value to particular operand.
      this operator is a binary operator. it is also known as a shorthand operators.
      there are following operators are used in assignment operators.
=, +=, -=, *=, /=, %=assignment operator in c

example :

      sum=sum+10
      sum+=10(assignment operator)

      sum=sum*10
      sum*=10(assignment operator)

      sum=sum%10
     sum%=10(assignment operator)

3. Relational operators :
      This operators defines the relation between two operands such as an operand is >= other operand.
      this operator is used to compare One operand to another operand. it is also use to compare arithmetic or logical expressions. It returns either true or false, or one or zero.
following are relational operators

      > greater than
      >= greater than or equal to
      < less than
      <= less than or equal to
      == equal to
      != not equal torelational operator in c

example :

      int x=10, y=20;

expressing        result
     x>y                  0
     x     x>=y                0
     x<=y                1
     x==y                0

4. Logical operators :
      Logical operator are used when to test more than one condition or expressions. it always returns 1 or 0. there are following operators are used in logical operators.
&&, ||, !logical operator in c

1.Logical AND(&&) :
      It will returns one when all expressions are written one otherwise it will return zero.
example :


      int a=10, b=20;

      a>=b && a<=20
          ___      ___
            0         1

         answer 0

      If first expression return 0 then it can not evaluate other expression, if first expression return one then it will evaluate another expression.

2. Logical OR(||) :
      This operator return one when either any expression return one otherwise is zero.
example :
      int x=10, y=20;

      x>=y || x<=y
       ___      ___
         0         1

       answer 1

      If 1 found in first expression then it does not evaluate other expression.

3. Logical NOT(!) :
      This operator returns opposite value of a particular expressions.
example :
      Expressions return one then answer is zero, if expression return 0 then answer is 1.

      int x=20, y=30;

      !(x>20) !(x<20)
         ____      ____
            0           1

           answer 1

5. Increment and Decrement operators :
      ++, -- is known as increment or decrement operators.
these operators are used when we want to increment or decrement value by 1.
      it is also known as a prefix or postfix operators.

1. prefix :
      ++x first add 1 to operand and result be assigning to variable on left operand is known as a prefix operator.
example :

      int x=10;

      y=++x;

     output x=11, y=11

2. postfix :
      x++ first assign the value to the variable on the left hand and then increment the operand is known as a postfix operator.
example :

      int x=10;

      y=x++;

     output x=10, y=11

      This operator also used into decrease the value from the number.
example :
      a--
      or
      --a

6. Conditional or Ternary operators :
      This operator is used to check conditions. it is also known as a ternary operator because it has required minimum 3 operands or expressions.

syntax :

      exp1 ? exp2 : exp3

example :

      int a, b=10, c=20;

      a=b>c?b:c

      If expression 1 return true then expression 2 will be evaluated otherwise expression 3 will be evaluated.
this is same as if, else statement.

      if(b>c)
         printf("%d",b);
      else
         printf ("%d",c);

7. Bitwise operators :
      This operator is used to check bit by bit value. this operator is used where our programming required testing of bit level. these operators are not applicable for float and double data type.
      following operators are used in bitwise operator.

      operators             meaning
           &                        bitwise AND
            |                        bitwise OR
            ^                       exclusive OR
            ~                      one's complement
            <<                     left shift
            >>                     right shift

1. bitwise AND (&) :
      This operator checks with bit by bit if both bits values are 1 then it will return 1 otherwise 0.

example :

      a= 1000
      b= 1111
           _____
 a&b= 1000

2. bitwise OR (|) :
      This operator check both bits if either bits has value 1 then it will return 1 otherwise 0.
example :

      a=1000
      b=1111
           _____
  a|b= 1111

3. exclusive OR (^) :
      In this operator exact one bit is 1 then result is 1 otherwise result is 0.
      if result is in odd position of 1 then it will return 1 otherwise 0.
example :

        a= 1101
        b= 0110
        c= 0101
           ______
a^b^c= 1110

4. one's complement :
      This operator convert 1 bit to 0 and 0 bit to 1. it is also known as a unary operator.
example :

      b= 1010
           ______
   ~b= 0101

5. left shift(<<) :
      In left shift operator left bits are move out and zeros are inserted at the right side.
example :
      If we have binary of 5 is 0101 then it perform left shift as per follow.

                0101 (binary of 5)
                _____
    5<<1= 1010 (binary of 10)

in short this operator perform multiply by 2 if 1 bit is shifted to the left.

6. right shift(>>) :
      In right shift operator right bits are moved out and 0 are inserted at the left side.
      this operator is used to perform division operations by 2 if 1 bit is shifted to the right.
      following example shows how to perform right shift operations.
example :

                 1010 (binary of 10)
                 ______
   10>>1= 0101 (binary of 5)

8. Special operator :
      Following operators are known as special operator.
1. sizeof
2. comma(,)

1. sizeof :
      This operators find out number of bytes occupy by the variable.
example :

      int a; printf("%d",sizeof(a)); output 2 bytes

      float b; printf("%d",sizeof(b)); output 4 bytes

      long double x; printf("%d",sizeof(x)); output 10 bytes

2. comma(,) :
      This operator is used to define more than one variable in a single statement.
example :

      int a,b,c;

« Data types in c language

» entry control and exit control loop in c language

Learn C programming language