Social Icons

Tuesday, December 4, 2012

Operators in C

To understand the different operators in 'C' , first let us understand about operators.Operator:- An operator is a symbol that operates on a certain data type and produces the output as the result of  operation. For example:-
                       x = y + z.
                      In this case:  '+' is the operator and y,z are operands.Now, let us know the different operators in 'C'. There are three categories of operators in 'C'. They are:
1. Unary Operators
                      It is an operator which operates on one operand or say it operates on itself. For example: increment and decrement operators. Examples: ++   --    !    etc.
2. Binary Operators
                      These operators are operators which operate on two operands. For example:   x+y. Here, '+' operats on two operands.
3. Ternary Operator
                           An operator which operates on three operands.

                Operators can also be classified on the basis of type of operations performed by them. They are as follows:-

1. Arithmetic Operators
                 Arithmetic operators include the basic addition(+), Subtraction(-), Multiplication(*) and Division(/) operations. One more operator is there called modulus operator(%). Hope, everyone is aware of +,-,*,/ operations. Here, the only new term is modulus operator. Let us understand this operator.
                        Modulus operator gives the remainder left on division operation. It is noted that its symbol is % but its not the percentage. For example:-
        10%3 = 1
Here, we divided 10 by 3 i.e 10/3. In this case, there will be quotient 3 and the remainder 1. So, remainder 1 will be the result. Some more examples are here:
        20%4 = 0
        25%7 = 4
        27%8 = 3

2. Relational Operators
                  These operators compare operands and return 1 for true or 0 for false. Examples of relational operators are:-
                 <    Less than                 >    Greater than
                <=   Less than or equal to
                >=   Greater than or equal to
                ==   Equal to
                !=     Not equal to


3. Logical Operators
                    These operators are used to compare or evaluate logical and relational expressions. There are 3 logical operators in 'C' language. They are:-
        &&  (Logical and) - In this case, if both conditions are true then result will be true.
        ||      (Logical OR) - In this case, if any condition is true between the two, result will be true.
        !       (Logical Not) - It inverts the condition. Makes true to false and false to true.


4. Assignment Operator
                   An assignment operator (=) is used to assign a value to a variable. It may be a constant value or a value of a variable. For example:-
            x = 5;     // Here, value 5 is copied to x
            x = a;    // Here, value of a is copied to x.

               Other assignment operators are:
              +=
              -=
              *=
              /=
             %=
                        These can be understood as follows:- Suppose we write
              x+=5;
               It means as x = x+5;
              Similarly,
              x-=10;   // It means x=x-10;

No comments:

Post a Comment

Total Pageviews