OPERATORS IN C

 OPERATORS:

                      Operators are the foundation of any programming language.We can define operators as a symbols that help us to perform sepcific mathematical and logical computations 
on operands .In other words we can say that an operator operators the operands .For example, ‘+’ is an operator used for addition, as shown below:  
       Ex:-c = a + b;
  • Here, ‘+’ is the operator known as the addition operator and ‘a’ and ‘b’ are operands. The addition operator tells the compiler to add both of the operands ‘a’ and ‘b’.  
  • C has many built-in operators and can be classified into 5 types:
            1.Arthematic operator.
            2.Relational operator.
            3.Logical operator.
            4.Bitwise operator.
            5.Assignment operator.
 
1.Arthematic operator:-      
        These operators are used to perform arithmetic/mathematical operations on operands. Examples: (+, -, *, /, %,++,–).


  • The operators +, - and * computes addition, subtraction, and multiplication respectively as you might  have expected.
  • Operators that operate or work with a single operand are unary operators. For example: Increment(++) and Decrement(–) Operator .
      Ex:- int val = 5;
               ++val;  // 6
  •  Operators that operate or work with two operands are binary operators. For example: Addition(+), Subtraction(-), multiplication(*), Division(/) operators.
      Ex:-   int a = 7;
               int b = 2;
               cout<<a+b; // 9
  • The modulo operator % computes the remainder. When a=9 is divided by b=4, the remainder is 1. The % operator can only be used with integers.
 2.Relational operator:-
                          A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0.
  • Relational operators are heavily used in decision-making and performing loop operations.


Ex:- int a = 3;
        int b = 5;
        a < b;
       // operator to check if a is smaller than b.


3.Logical operator:-
                          An expression containing a logical operator in C language returns either 0 or 1 depending upon the condition whether the expression results in true or false. Logical operators are generally used for decision-making in C programming.

                    

k
Ex:-  (4 != 5) && (4 < 5);     // true

4.Bitwise operator:-
                  The Bitwise operators are used to perform bit-level operations on the operands. The operators are first converted to bit-level and then the calculation is performed on the operands. Mathematical operations such as addition, subtraction, multiplication, etc. can be performed at the bit level for faster processing.

                      

Ex:- int a = 5, b = 9;   // a = 5(00000101), b = 9(00001001)
        cout << (a ^ b);   //  00001100
        cout <<(~a);       // 11111010


5.Assignment operator:-
                        Assignment operators are used to assign value to a variable. The left side operand of the assignment operator is a variable and the right side operand of the assignment operator is a value. The value on the right side must be of the same data type as the variable on the left side otherwise the compiler will raise an error. 
 
                        

Ex:-   “+=”
        This operator is the combination of the ‘+’ and ‘=’ operators. This operator first adds the current              value of the variable on left to the value on the right and then assigns the result to the variable on            the left. 

      (a += b) can be written as (a = a + b)
      If initially value stored in a is 5. Then (a += 6) = 11.


Comments