Sunday 16 August 2015

C language Operators and Expressions Tutorials

Operators:

   The symbol is used to perform an operation is called an operator.

--> An operator which expect only one operand is referred as Unary operator.

--> An operator which expect two operands is referred as Binary operator.

--> An operator which expect three operands is referred as Terinary operator.

--> The operators provided by C language are classified as follows.

1. Size operator:

   This operator is useful to retrieve the size of a datatype or a variable.

operator:    sizeof

   Syntax:

      sizeof( datatype/variable )

Program:

#include< stdio.h >

#include< conio.h >

void main( )

{

      int x;

      clrscr( );

      x = sizeof(char);

      printf("\n\tCHAR: %d",x);

      x = sizeof(int);

      printf("\n\tINT: %d",x);

      x = sizeof(float);

      printf("\n\tFLOAT: %d",x);

      x = sizeof(double);

      printf("\n\tDOUBLE: %d",x);

      x = sizeof(long int);

      printf("\n\tLONG INT: %d",x);

      x = sizeof(long double);

      printf("\n\tLONG DOUBLE: %d",x);

}

2. Arithmetic operators:

   These operators are useful to perform arithmetic operators on the data.

--> The arithmetic operators are provided by C language as follows.

      + ------> Addition

      - ------> Subtraction

      * ------> Multiplication

      / ------> Division

--> When both the numerator and denomenator are integers, the division operator give only the quotient and will not considered the remainder

      % ------> Modulo or Remainder

--> It gives the remainder when two numbers are divided.

Program:

#include< stdio.h >

#include< conio.h >

void main( )

{

      int x, y, a, b, c, d, e;

      clrscr( );

      printf("\nEnter two numbers: ");

      scanf("%d %d", &x, &y);

      a = x + y;

      b = x - y;

      c = x * y;

      d = x / y;

      e = x % y;

      printf("\n\t %d + %d : %d", x, y, a);

      printf("\n\t %d - %d : %d", x, y, b);

      printf("\n\t %d * %d : %d", x, y, c);

      printf("\n\t %d / %d : %d", x, y, d);

      printf("\n\t %d %% %d : %d", x, y, e);

}

Note:

--> When % is to be displayed in the output, we are required to specify %% in the format.

      + = ---> Add and Assign

      - = ---> Subtract and Assign

      * = ---> Multiply and Assign

      / = ---> Divide and Assign

      % = ---> Modulo and Assign

3) Increment/Decrement operators:

      ++ ----> Increment operator

      --   ----> Decrement operator

      x = x + 1 or x += 1 or x++

      x = x - 1 or x -= 1 or x--

--> Both the Increment and Decrement operators can be used in two ways as follows.

      x++ ---> post-increment

      ++x ---> pre-increment

      x--   ---> post-decrement

      --x   ---> pre-decrement

--> When we used as individual statement, both the pre and post operators appear to be same.

--> They vary when we used in other expressions or statements

--> When we used in expressions and other statements.

1) The pre-increment/pre-decrement assume latest updated value in that expression/statements

2) The post-increment/post-decrement assume pre-updated value or old value is that expression.

--> But in both the cases the value of that variable will be increment.

Program:

#include< stdio.h >

#include< conio.h >

void main( )

{

      int x;

      clrscr( );

      printf("\nEnter a number: ");

      scanf("%d", &x);

      printf("\n\t%d %d %d %d %d", x++, ++x, x++, ++x, x++);

}

4) Type casting operator:

This is useful when a value of one type is to be consider as of another type temporarily for that expressions.

   Syntax:

      (type) value

Program:

#include< stdio.h >

#include< conio.h >

void main( )

{

      int x, y;

      float res;

      clrscr( );

      printf("\n\tEnter two numbers: ");

      scanf("%d %d", &x, &y);

      res = (float)x/y;

      printf("\n\tRESULT: %.2f", res);

}

5) Bit-wise operators:

These operators are useful to perform operations on the individual bits of the given data.

--> C language has a distinction of supporting special operators known as "Bit-wise operators" for manipulating of data at "bit level"

--> These operators are used for "testing the bits" or "shifting them" left or right--> Bit-wise operators may not be applied in "float or "double". They operate only "integers". Such as int, char, short, long int etc.

      operator      meaning

           >>           Right shift

           <<           Left shift

            ^            Bit-wise XOR

            ~            1's complement

            &            Bit-wise AND

            |             Bit-wise OR

--> Bit-wise operators are used for operators on individual bits.

1) Bit-wise AND:

This is useful to perform AND operator on the individual bits of the given data.

--> The truth table of Bit-wise AND operator is

    a      b      a&b

    0      0        0

    0      1        0

    1      0        0

    1      1        1

a = 0 0 0 0 0 1 1 1

b = 0 0 0 1 0 0 0 1

-------------------------

a&b = 0 0 0 0 0 0 0 1

-------------------------

2) Bit-wise OR operator:

This is useful to perform OR operation on the individual bits of the given data.

--> The truth table of Bit-wise OR operator is

    a      b      a|b

    0      0        0

    0      1        1

    1      0        1

    1      1        1

a = 0 0 0 0 0 1 1 1

b = 0 0 0 1 0 0 0 1

-------------------------

a|b = 0 0 0 1 0 1 1 1

-------------------------

3) Bit-wise XOR:

This is useful to perform XOR operation on the individual bits of the given data.

--> The truth table of Bit-wise XOR operator is

    a      b      a^b

    0      0        0

    0      1        1

    1      0        1

    1      1        0

a = 0 0 0 0 0 1 1 1

b = 0 0 0 1 0 0 0 1

-------------------------

a^b = 0 0 0 1 0 1 1 0

-------------------------

4) one's complement:

This is useful to perform 1's complement on the internal representation of the given data.

--> It is used to perform a unary operator.

a = 0 0 0 0 0 1 1 1

-------------------------

~a = 1 1 1 1 1 0 0 0

-------------------------

5) Left shift operator(<<):

This is useful to shift the bits in the internal representation of the given data towards left by the specified number of positions.

--> If the value is shifted by n positions towards left, then the value is raised by 2n

a = 0 0 0 0 1 1 0 1

a<<2

a = 0 0 1 1 0 1 0 0

6) Right shift operator(>>)

This is useful to shift the binary representation of the given value towards right by the specified number of positions.

--> If the value shifted by n-positions towards right, then the value will be decreased by 2n

a = 0 0 0 0 1 1 0 1

a>>2

a = 0 0 0 0 0 0 1 1

6) Relational operators:

These operators are useful to find the relation between the two given numbers.

      <     ---> Lessthan

      <=   ---> Lessthan or equal to

      >     ---> Greaterthan

      >=   ---> Greaterthan or equal to

      ==   ---> Equal to

      !=    ---> Not equal to

--> The relation operators are all Binary operators

--> They result in an integer value. That integer value will be either 0 or 1

--> '0' represents false i.e the condition doesn't hold

--> '1' represents true i.e the condition is satisfied

Note:

--> = is called Assignment operator.

--> It is useful to assign the RHS value to LHS variable

--> == is called comparison operator

--> It compares both the RHS and LHS value and return TRUE(1) if they are same, FALSE(0) otherwise.

7) Conditional operator:

--> The conditional operator is represented as "?" and ":"

   Syntax:

      (condition)?s1:s2

--> The condition is evaluated first

--> If it is TRUE, then the statements between ? and : will be executed.

--> If it is FALSE, then the statement after : will be executed.

Note:

--> "0" is considered as FALSE and any nonzero value is considered as TRUE.

--> We can specify as many number of statements as possible and required between ? and : they should be seperated by ",".

--> We can also specify as many number of statements as possible and required after column.

--> Among the statements specify after column the first statement will be executed only when the condition is false and the remaining statements will be executed all the times i.e when the condition is true or false.

--> Conditional operators can be nested i.e we can use conditional operator in another conditional operator.



0 comments:

Post a Comment