Saturday, May 16, 2020

Operators in C






As we have seen the various types of Variables which are used in C in the previous content. Now one Question may arise in your mind that what type of operations are performed on those variables? So, before knowing the answer of this question you must understand the concept of operators because an operation is performed with the help of operator.
What is Operator?
                                                Remember your basic mathematical operations which are used to perform the operations (Addition, Subtraction, Multiplication, and Division) on the numeral values. In mathematics whenever we needed to compare two values we used the Relational operators. After thinking about these things the images of the various operators comes in your mind.
                                                In C an operator is a symbol that tells the computer to perform certain mathematical or logical operations/manipulations. We know that in computer whenever we want to perform some specific task we need a program which is the set of instructions. When we write a program we need to use the various operators according to the task to manipulate data and the variables. Operators are the part of the mathematical and logical expression. You can understand the importance of the operator in the mathematics, think if you were not had the plus ‘+’ operator then how could you add the two numbers? C provides seven types of operators which are frequently used in the program. These are as follows:-
             1.      Arithmetic operators
            2.     Relational operators
            3.      Logical operators
            4.     Bitwise operators
            5.      Assignment operator
            6.     Increment & Decrement operators
            7.      Conditional /Ternary operators
            8.     Other operators
Now, you will understand all the above one by one easily in
Detail with the various C examples and will be able to get the concept of the operator.
  ·        Arithmetic operators:-
                                    C provides all the basic arithmetic operators. These operators works same as we use in daily life and do in other programming languages. These can operate on any built in data types allowed in C. The arithmetic operators can be classified into Unary & Binary arithmetic operators (Discussed later). A list of arithmetic operators and their meanings are given below:-
Operators                                                    Meaning
         ‘+’                                                 Addition or unary plus
          ‘-’                                                  Subtraction or unary minus
          ‘*’                                                 Multiplication
                                               ‘/’                                                   Division
                                             ‘%’                                                Modulus division
            Example:
Let a and b be declared by the statement.
            int a  = 10, b = 5; (Values are declared and initialized)
            Then the arithmetic operations gives the following results:
                                 a + b = 15
a – b = 5
a * b = 50
a / b = 2
a % b = 0 (remainder is 0, Zero)
Note: -> [The unary minus operator has effect of multiplying its operand by -1. In integer            division operation, the result is truncated towards zero if both the operands are of the same sign, and is dependent on the machine if one of the operand is negative. ]
            Example:
                        10 / 12 = 0
                        -10 / -12 = 0
                        -10 / 12 = 0 or -1       (The result is machine dependent)
        ·        Relational operators:-
While arithmetic operators are used to evaluate arithmetic
Expressions, relational operators are used to compare arithmetic, logical and character expressions. To compare two similar quantities and, depending on their relation, to take some decision we take the help of the relational operators. Each of these operators compare their left-hand side with their right-hand side. The result of the relational expression is either ‘1’ when the expression is true, or ‘0’ when the result of the expression is false, that is an integer value.
The six relational operator and their meaning are as follows:
                                    Operators                                        Meaning                                                                  
                                             ‘<’                                                      Less than.
                                             ‘>’                                          Greater than.
                                             ‘<=’                                         Less than equal to.
                                             ‘>=’                                         Greater than equal to.
                                             ‘==’                                        Equal to.
                                             ‘!=’                                         Not equal to.
                                                            To get the concept of the relational operators easily, it is necessary to know the fundamental of the If statement. Considering a simple example that uses the relational operator in a if statement. The If statement will be discussed in the next content, so we see here the basic idea of the if function. The If statement evaluates the expression and evaluates the next statement only if the expression evaluates to true. The given example compares the two numbers taken from the user:
            #include<stdio.h>                                                                    
            Void main ()
            {
                        int x , y ;
                        printf (“Enter the two numbers. \n”);
                        scanf (“%d %d”, &x, &y);
                        if (x == y)
                                                printf (“True”);
                        else
                                                printf (“False”);
}
In the above example the If statement checks the condition within the braces (which is x == y) is true, just after the If statement is executed, otherwise the else statement is executed.
Note: [When using relational operators, the signed or unsigned nature of the numbers being compared becomes important. Neglecting this fact can lead to hard to find the errors.]
     ·        Logical operators:
In C logical operators are used when we check two or more relational
conditions in a single expression. An expression involving && or || is sometimes called compound expression. A logical expression also gives the value of 1 or 0 as the result, according to the truth table. The following logical operators are as follows:
                                    Operators                                        Meaning
                                         ‘&&’                                   Logical AND.
                                           ‘||’                                                            Logical OR.
                                            ‘!’                                           Logical NOT                       
Now let’s see the example of each logical operators for better understanding:
            Logical AND:
                                    Consider the following expression:
                        x < y && x < z
                                                The above expression evaluates to true if and only if both the (x < y and x < z) expression are true.
            Logical OR:
                                    Consider the following expression:
                        x < y || x < z
                                                The above expression to true if any one of the expression is true.
            Logical NOT:
                                    The NOT ( ! ) operator takes single expression and evaluates to true if the expression is false and evaluates to false if the expression is true. You would have remember the function of the NOT gate that was the just reverse of the input. Consider the following expression:
                        ! (x < y)
                                    The expression after the NOT operator evaluates to true only if the value of x is greater than the value of y. This operator is convenient to use when you want to test whether the value of a variable is zero.
     ·        Bitwise operators:
 The Bitwise operators are very powerful operator provided by C for
 manipulation of data at bit level. These operators are used for testing, complimenting, or shifting bits to the right or left. Usually bitwise operators are not useful in cases of float and double variables. The bitwise operators are as follows:
                                    Operators                                        Meaning
                                             ‘&’                                              Bitwise AND.
                                              ‘|’                                                 Bitwise OR.
                                              ‘^’                                                Bitwise XOR.
                                              ‘<<’                                               Left shift.
                                              ‘>>’                                               Right shift.
                                               ‘~’                                               bitwise compliment.
            Now let’s see the examples of each for the better understanding.
            Consider the statement:
                                                            int a = 5, b = 2, c;
            Bitwise AND:
                                    a          0000 0000 0000 0101
                                    b          0000 0000 0000 0010
         a  & b         0000 0000 0000 0000
                                                                                                Here the result will be 0 because we know that the AND operation becomes true only if both the inputs are true.
            Bitwise OR:
                                    a          0000 0000 0000 0101
                                   b          0000 0000 0000 0010
                  a  | b          0000 0000 0000 01 1 1    
                                                                                                Here the result will be 0111 decimal equivalent to 7, because the OR gate results true if any of the input is true.
            Bitwise NOT:
                                    a          0000 0000 0000 0101
                        b          0000 0000 0000 0010
                a  ^ b         0000 0000 0000 01 1 1
                                                                                                Here the result will be 0 1 1 1 which is decimal equivalent to 7, because we know that the result of the XOR becomes false when all the inputs are false.
            Bitwise Left Shift:
                                                When we shift to left the left most bit drop off due to the left shift. These zeros are inserted in the right. When one bit is shifted left the shifted value is doubled. Means if 10 is shifted one bit left, after the shifting the values the new value will be 20. Let’s see this at bit level:-
                                    0000 0000 0000 1010 = 10
            Drop off <-    0000 0000 0001 0100 = 20 Here one bit is shifted to the left means 0 is inserted in the right most.  
            Bitwise Right Shift:
                                                When a value is shifted one bit to the right the position is to perform integer division by 2. Hence, shifting to the right by 2 bit the position has the effect of integer division by 2 * 2 = 4. And so on. Let’s see the example shifting one bit to the right of the value 20:
                                    0000 0000 0001 0100 = 20
                                    0000 0000 0000 1010 = 10 -> Drop off
        Bitwise Compliment:
                                     This operator is a unary operator. It gives the value got by complimenting each bit of the operand.
     ·        Assignment Operators: 
The Assignment operators are used to assign the result of an
 expression to a variable. This operator evaluates the expression on the right and assign the resulting value to the left side variable.
                                                                        The statement a+=5; will add the number 5 to the value of a and assign the value to the a. The statement a+=5; is the same as a= a + 5;. The other assignment operators are as follows:
                                    Operators                                                    Meaning
                                             -=                                                              v2 – v1 = v2
                                             /=                                                              v2 / v1 = v2
                                            *=                                                             v2 * v1 = v2
                                            |=                                                              …….
                                            &=                                                          …….
                                           <<=                                                            …….
                                           >>=                                                            …….
     ·        Increment & Decrement operators:
 These are very useful operators and found in only C
Language. These are unary operators and take the form ++x (Pre-increment) and x++ (Post- increment) and –-x (Pre- decrement) and x--(Post-decrement).
                                                                                    These both operators are basically used in For and While loops. Consider the following:
                                                                        x = 5;
                                                                        y = x++;
                                                                        In this case the value of y would be 5 and x would be 6. Now consider the following:
                                                                        x = 5;
                                                                        y = ++5;
                                                                        In this case the value of y and x would be 6. A prefix operator first adds 1 to the operand and then the result is assigned to the variable o left. On the other hand, a postfix operator first assigns the value to the variable on the left and then increments the operand.
     ·        Conditional/Ternary Operator:
This operator is available in to construct conditional
 expressions of the form:
                                                expn1 ? expn2 : expn3
 Which is also called the Ternary operator. The operator ‘ ? ’ works as follows:
                                                                        When this statement is executed then the first expression expn1 is evaluated and if it is true then, the expn2 is evaluated and become the value of the expression. If expn1 is false, expn3 is evaluated and its value becomes the value of the expression. Consider the following expression:
                                                a = 10 , b = 15;
                                                x = a > b ? a : b; // Here the output will be 15//
                                                Note that only one expression (either expn2 or expn3) is evaluated after the evaluation of expn1. It can be understood by these statements:
                                                If (a > b)
                                                            x = a;
                                                else
                                                            x = b;
    ·        Other operators:
Another operator is the member selection operator the dot (.) and
 indirection (->) operator, which is used with the Structure and the Unions. ( Will be discussed  later).
Click to go to the previous content. (https://heylearnc.blogspot.com/2020/04/learn-c-history.html)

By : - Ashwini kumar vishwakarma
(Keep sharing, thanks!!)