Assignment Operators in C with Examples

This C Tutorial Explains Different Assignment Operators in C with Examples.

What is Assignment Operator in C?
The basic assignment operator in C Programming Language is ‘=’, which we know as “equal to” in Mathematics. However, in C this is not to be confused with “equal to” but this performs different operation on its operands. This operator assigns value of expression/variable/constant at its right to the variable at its left. Every expression in C evaluates to some value.

Different types of assignment operators in C are:

  • Addition Assignment (+=): This operator adds the value of Right hand quantity to the Left Hand variable and places the result in the Left Hand variable.
  • Subtraction Assignment (-=): This operator subtracts the R–H quantity from the L–H variable and places the result in the L–H variable.
  • Multiplication Assignment (*=): This operator multiplies the L–H variable by the R–H quantity and places the result in the L–H variable.
  • Division Assignment (/=): This operator divides the L–H variable by the R–H quantity and places the result in the L–H variable.
  • Remainder Assignment (%=): This operator gives the remainder from dividing the L–H quantity by the R–H quantity and places the result in the L–H variable.
  • Bitwise AND Assignment (&=): This operator assigns bitwise & of L–H and R–H to the L–H quantity/l-H variable.
  • Bitwise OR Assignment (|=): This operator assigns bitwise | of L–H and R–H to the L–H quantity/L–H variable.
  • Bitwise XOR Assignment (^=): This operator assigns bitwise ^ of L–H and R–H to the L–H quantity/L–H variable.
  • Right-shift Assignment (>>=): This operator assigns L–H >> R–H to the L–H quantity and places the result in the L–H variable.
  • Left-shift Assignment (<<=): This operator assigns L–H << R–H to the L–H quantity and places the result in the L–H variable.

For example: Valid assignment statements.

    result = (x * y) / (u + v); 
    value = x + y;
    more = ++yes;
    int const TRUE = 1;
    int const FALSE = 0;
 
    /* 
     * In C, variable names should be in lower case. However, Constants
     * are named in Capitals. Above, TRUE and FALSE are constants.
     */
  • The right side value of the assignment is called rvalue and left side of the assignment is called lvalue or modifiable lvalue.
  • lvalue refers to memory location or region of actual data storage or data object to hold value into while rvalue refers to value of some expression or variable or constant which is to be assigned to lvalue.
  • Not all data objects can have their values changed therefore data objects that can change values are called modifiable lvalues. lvalue can not be a constant.

Example 1:

    56 = x + y + z;
    /* 
     * since lvalue must be a location to hold value into it. 56 is not a
     * location, instead this is a value. Not a valid assignment statement
     */

Example 2:

advertisement
advertisement
  1. /* assignment.c -- program displays use of assignment operator */
  2. #include <stdio.h>
  3. int main(void)
  4. {
  5.     int one, two, three;
  6.     one = two = three = 68; 
  7.  
  8.     /* one, two and three, each assigned 68 */
  9.     /* associativity of assignment operator is from right to left */
  10.  
  11.     printf("                   one  two   three");
  12.     printf("First round score %4d %8d %8d\n", one, two, three);
  13.  
  14.     return 0;
  15. }

Operator Precedence
Consider the following line of code:

    result = 19.0 + 20.0 * n / SCALE;

The statement “result = 19.0 + 20.0 * n / SCALE;” has an addition, a multiplication, and a division operation.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

Which operation takes place first? Is 20.0 added to 19.0, the result of 39.0 then multiplied by n, and that result then divided by SCALE? Is 20.0 multiplied by n, the result added to 19.0, and that answer then divided by SCALE? Is it some other order? Let’s take n to be 6.0 and SCALE to be 2.0. If you work through the statement using these values, you will find that the first approach yields a value of 117.0. The second approach yields 69.5. A C program must have some other order in mind, because it would give a value of 79.0 for result.

Clearly, the order of executing the various operations can make a difference, so C needs unambiguous rules for choosing what to do first. C does this by setting up an operator precedence order. Each operator is assigned a precedence level. As in ordinary arithmetic, multiplication and division have a higher precedence than addition and subtraction, so they are performed first. What if two operators have the same precedence? If they share an operand, they are executed according to the order in which they occur in the statement. For most operators, the order is from left to right. (The = operator was an exception to this.) Therefore, in the statement

    result = 19.0 + 20.0 * n / SCALE;

The order of operations is as follows:

advertisement

20.0 * n

The first * or / in the expression (assuming n is 6 so that 20.0 * n is 120.0)

120.0 / SCALE

Then the second * or / in the expression

19.0 + 60

advertisement

Finally (because SCALE is 2.0), the first + or – in the expression, to yield 79.0

Operators in Order of Decreasing Precedence

  Operators                          Associativity
 
    ()                                Left to right
    + - (unary)                       Right to left
    * /                               Left to right
    + - (binary)                      Left to right
    =                                 Right to left

More Assignment Operators: +=, -=, *=, /=, %=

C has several assignment operators. The most basic one, of course, is =, which simply assigns the value of the expression at its right to the variable at its left. The other assignment operators update variables. Each is used with a variable name to its left and an expression to its right. The variable is assigned a new value equal to its old value adjusted by the value of the expression at the right. The exact adjustment depends on the operator. For example,

  1.     scores += 20; is the same as scores = scores + 20;
  2.     dimes -= 2; is the same as dimes = dimes - 2;
  3.     bunnies *= 2; is the same as bunnies = bunnies * 2;
  4.     time /= 2.73; is the same as time = time / 2.73;
  5.     reduce %= 3; is the same as reduce = reduce % 3;

Well! The preceding list uses simple numbers on the right, but these operators also work with more elaborate expressions, such as the following:

    x *= 3 * y + 12;
    x = x * (3 * y + 12); /* both statements are same */

The assignment operators we’ve just discussed have the same low priority that = does—that is, less than that of + or *. This low priority is reflected in the last example in which 12 is added to 3 * y before the result is multiplied by x.

You are not required to use these forms. They are, however, more compact, and they may produce more efficient machine code than the longer form. The combination assignment operators are particularly useful when you are trying to squeeze something complex into a for loop specification.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.