Thursday, January 28, 2016

Assignment Operator

Assignment operator assigns value of the expression on the right side to left side variable. The base assignment operator is '='. In C, you can use this operator like the following variable = expression
Here variable can be any kind of a variable and expression can be a simple constant, another variable or may be a more complex expression, like a formula. The value of the expression will be evaluated and assigned to the variable. But, there are some things to note about the assignment operator.


If the value of the expression on the right side is not same data type as variable on the left, then the value of the expression will be converted to same data type as the variable. Suppose, the expression is a floating point number and the variable is an integer. Then, the floating point number will be converted to integer and then will be assigned to the variable. It is better to use same data types on both sides unless it is a must, else it may lead to information loss.

C also allows multiple assignment statement like variable1 = variable2 = ... = variableN = expression.


This means, the value of the expression will be assigned to 'variableN', which in turn will be assigned to the variable left of 'variableN' and it propagates till 'variable1'. So, after the execution of multiple assignment operation, all the variables will have the same value.

Apart from the base assignment operator explained above, C also has different variants of the same assignment operator. They are  '+=', '-=', '*=', '/=', '%='. The behaviour of these operators is explained below.
OperatorNameDescriptionExample
+=Add and Assignment OperatorThis operator perform addition of operand on left side with operand right side and the result will be assigned to operand at left side.a += 10: This is equivalent to a = a + 10
-=Subtract and Assignment OperatorThis operator subtracts the operand on right side from the operand on left side and the result will be assigned to left side operand.a -= 10: This is equivalent to a = a - 10
*=Multiplication and Assignment OperatorThis operator multiplies the operand on right side with the operand on left side and the result will be assigned to left side operand.a *= 10: This is equivalent to a = a * 10
/=Division and Assignment OperatorThis operator divides the operand on left side with the operand on right side and the result will be assigned to left side operand.a /= 10: This is equivalent to a = a / 10
%=Modulus and Assignment OperatorThis operator computes the modulus of left side operand by right side operand and the result will be assigned to left side operand.a %= 10: This is equivalent to a = a % 10

Examples of Assignment Operator

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. int a = 25, b = 10;
  6. a += b;
  7. printf("'a' after a += b: %d\n", a);
  8. a = 25, b = 10;
  9. a -= b;
  10. printf("'a' after a -= b: %d\n", a);
  11. a = 25, b = 10;
  12. a *= b;
  13. printf("'a' after a *= b: %d\n", a);
  14. a = 25, b = 10;
  15. a /= b;
  16. printf("'a' after a /= b: %d\n", a);
  17. a = 25, b = 10;
  18. a %= b;
  19. printf("'a' after a \%= b: %d\n", a);
  20. return 0;
  21. }
  22.  
  23. Output:
  24. 'a' after a += b: 35
  25. 'a' after a -= b: 15
  26. 'a' after a *= b: 250
  27. 'a' after a /= b: 2
  28. 'a' after a %= b: 5

No comments: