Assignment Operators

Assignment Operators
C में कुल मिलाकर 11 assignment operators है । = basic assignment operator है।
There are overall eleven assignment operators in C. The = is the basic assignment operator. The general form of using this operator is –
 इस operator को उपयोग करने का सामान्य तरीका -
variable = expression;
For example in the following statement –
x = 25;
the = operator directly copies the 25 in x. Please note that the 25, which is an integer constant, is an expression here. Similarly, in the following –
x = y;
y is an expression. The assignment operator copies the value of y into x.
But, in the statement here, it works at last, when the expression is solved to a value –
x = 2*((3*(20/5) + 3) - 2);         Original Expression
x = 2*((3*4 + 3) - 2);                 Innermost parenthesis (20/5) solved
x = 2*((12 + 3) - 2);                   * operator is solved
x = 2*(36 – 2);                           Intermediate parenthesis (12+3) solved
x = 2*34;                                   Outermost parenthesis solved
x = 68;                                       * operator is solved

Now, the = operator works. It copies the result 68 into x. There is one more thing which we would like to specify about assignment operator. Read this.
In algebra if you write x = x + 4, then, it will be an equation. And, for every value of x the result of the equation will be false. No value of x can satisfy this equation. But in C, if you write x = x + 4, it will not be an equation. Rather, it will be an assignment statement. Because, = is assignment operator in C. Now understand how x = x + 4 is solved. In this statement, the value of x is first replaced at RHS. Then, added with 4. After addition, the result is assigned back to x. Thus, increase the value of x by 4. If x = 10, then the following steps show how this is done.
x = x + 4 Original statement
x = 10 + 4 x is replaced by its value 10
x = 14 Addition performed, Now x holds 14.
Now, you can easily understand that statements like x = x * 4 or x = x / 4 are perfectly valid in C. The statement x = x * 4 will make the value of x four times the original one. On the other hand, x = x / 4 will reduce x to one fourth of its original value.


*/ Program to interchange the constants of num1 and num2 */

                 #include<stdio.h>

                #include<conio.h>

                int main(void)

               {
                     int num1, num2, num3;
                     clrscr();
                     printf("Enter the value of num1 & num2");
                     scanf("%d%d",&num1,&num2);
                     num3= num1;
                     num1= num2;
                     num2 = num3;'
                     printf("The interchange value of num1 =%d\n", num1);
                     printf("The interchange value of num2 =%d\n", num2);
                  getch();
                 return 0;




             }
Output :
Enter the value of num1 & num2 10 20
The interchange value of num1 = 20
The interchange value of num2 = 10

The other assignment operators are known as Shorthand Assignment operators or Compound assignment operators. They are discussed here.

Shorthand Operators or Compound Assignment Operators  
There are ten shorthand (or compound) assignment operators in C. They are divided into two subgroups –
Arithmetic assignment operators ( +=, -=, *= , /=, %=)
Bitwise assignment operators (<<=, >>=, &=, |=, ^=)
Here we are introducing Arithmetic assignment operators. Bitwise assignment operators will be discussed after the discussion of bitwise operators.
Using shorthand operators you can “transform” a simple assignment statement into an advanced assignment statement. That is, a simple assignment statement of the form –
variable = variable operator expression      (first form)
can be written as –
variable operator= expression                      (second form)
In first form variable is a variable which must be common in left and right hand side and operator is any arithmetic operator. The expression must evaluate to a value. If these conditions meet, the first form can be converted to second form. 
You can easily understand that the first form is transformed to second form by eliminating the variable from right and adjusting the operator with the assignment operator as shown.

Consider a statement –
x = x + 5;
Here x is common in both LHS and RHS. So using Shorthand Technique you can rewrite it by eliminating the x from RHS and adjusting the + operator with =, like this –
x += 5;
That is
x = x + 5;
is transformed like –
                                    x  += 5;
Notice, how the + is combined with =. The combination += is the addition assignment operator. That is, it adds the value at right with the value of variable at left and stores the result back into the variable.
For Example –
If x has value 10 then after a statement like this--
x += 5;
the value of x would be 15. Because x  += 5 is solved just as –
x = x + 5;
In the statement just shown, first the value of x is replaced at RHS so the statement becomes
x = 10 + 5;
and then
x = 15;
Now, x stores the value 15, which is 5 more than its previous value 10. The previous value 10 has been destroyed.
Please note that += is a combination of two symbols + and = but treated whole as an operator. There should be no space between + and =. That is, you cannot write x +=5 as x + = 5; as described, space is not allowed between + and =.  
The shorthand technique is applicable only when the variable at left of = exists at right. For example the statement x = x + 10 can be written as x += 10 because the variable x, which is at left of =, exist at right. But there is no way to write the following statement in shorthand –
x = y + 10;
because the variable x which is at the left of =, is not at right of =. 
If a long expression is used at right of = then the shorthand technique can be applicable if the expression can be converted to variable operator expression form.
For example, if x = 10, y = 20 and z = 30 then
x = x + y + z;
can be written as x = x + (y + z). This matches the form variable = variable operator expression and further it can be written as –
x += (y + z);       or             x += y + z;
As you know all assignment operators work when the expression at right is solved completely and a single value is obtained (see Precedence Table). So the x += y + z first turns to x += 20 + 30 and then to x += 50. Now, the += works and the value of x (which is currently 10) is increased by 50. That is, the final value of x becomes 60. If you solve the original expression x = x + y + z then you will see that the result obtained by x is 60, too. As given here –
x = x + y + z; Original Expression
x = 10 + 20 + 30  Values Replaced
x = 60 x holds 60
As you can see that the resultant values in x are same from original expression x = x + y + z and shorthand form x += y +z. Therefore, x += y + z is the correct shorthand form of x = x + y + z.
 Now, consider one more example – the expression x = x * y + z can not be written as x = x * (y +z ) therefore the expression x = x * y + z can not be converted to match the form variable = variable operator expression. By mistake, if you write the x = x * y + z as x*  = y +z then it will be wrong. Because x *= y + z means x = x * (y + z) not x = x * y + z. Also, you can see that there is a lot of difference between the values from original expression and shorthand form. As given here –


    Original Expression
x = x * y + z;
x = 10 * 20 + 30
x = 200 + 30
x = 230

Here the result is 230
Shorthand Expression
x *=  y + z;
x *= 20 + 30
x *=  50
x  = 500 value of x is now 50 times the original value                               
The original value was 10.
Here the result is 500.

You can see the difference between the results of original expression and shorthand expression. So writing x = x*y + z as x *= y + z is wrong.

Similarly, the other operators -=,*=, /=, %= can be used to create a Shorthand Form of a simple   assignment statement. Here is a table for their name.
Operator
Name
+=
Addition Assignment
-=
Subtraction Assignment
*=
Multiplication Assignment
/=
Division Assignment (assigns quotient)
%=
Modulo Assignment (assigns remainder)

Here is a table to illustrate the use of Shorthand Operators.

Simple Assignment Statement
Statement using Shorthand Operator
x = x + 5
x += 5
x = x - 5
x -= 5
x = x * 5
x *= 5
x = x / 5
x /= 5
x = x % 5
x %= 5





The following diagram will help you to memorize the transformation of a simple assignment statement into its shorthand equivalent.




1 comment: