We have already used the assignment operator ( `=`

) several times before. Let’s discuss it here in detail.

The assignment operator ( `=`

) is used to assign a value to the variable. Its general format is as follows:

1 |
variable = right_side |

The operand on the left side of the assignment operator must be a variable and operand on the right-hand side must be a constant, variable or expression. Here are some examples:

1 2 3 |
x = 18 // right operand is a constant y = x // right operand is a variable z = 1 * 12 + x // right operand is an expression |

The precedence of the assignment operator is lower than all the operators we have discussed so far and it associates from right to left.

We can also assign the same value to multiple variables at once.

1 |
x = y = z = 100 |

here `x`

, `y`

and `z`

are initialized to `100`

.

Since the associativity of the assignment operator ( `=`

) is from right to left. The above expression is equivalent to the following:

1 |
x = (y = (z = 100)) |

Note that expressions like:

1 2 3 |
x = 18 y = x z = 1 * 12 + x |

are called assignment expression. If we put a semicolon(`;`

) at the end of the expression like this:

1 2 3 |
x = 18; y = x; z = 1 * 12 + x; |

then the assignment expression becomes assignment statement.

## Compound Assignment Operator

Assignment operations that use the old value of a variable to compute its new value are called Compound Assignment.

Consider the following two statements:

1 2 |
x = 100; x = x + 5; |

Here the second statement adds `5`

to the existing value of `x`

. This value is then assigned back to `x`

. Now, the new value of `x`

is `105`

.

To handle such operations more succinctly, C provides a special operator called Compound Assignment operator.

The general format of compound assignment operator is as follows:

1 |
variable op= expression |

where `op`

can be any of the arithmetic operators (`+`

, `-`

, `*`

, `/`

, `%`

). The above statement is functionally equivalent to the following:

1 |
variable = variable op (expression) |

**Note**: In addition to arithmetic operators, `op`

can also be `>>`

(right shift), `<<`

(left shift), `|`

(Bitwise OR), `&`

(Bitwise AND), `^`

(Bitwise XOR) . We haven’t discussed these operators yet.

After evaluating the expression, the `op`

operator is then applied to the result of the expression and the current value of the variable (on the RHS). The result of this operation is then assigned back to the variable (on the LHS).

Let’s take some examples:

The statement:

1 |
x += 5; |

is equivalent to `x = x + 5;`

or `x = x + (5);`

.

Similarly, the statement:

1 |
x *= 2; |

is equivalent to `x = x * 2;`

or `x = x * (2);`

.

Since, `expression`

on the right side of `op`

operator is evaluated first, the statement:

1 |
x *= y + 1; |

is equivalent to `x = x * (y + 1)`

.

The precedence of compound assignment operators are same and they associate from right to left (see the precedence table).

The following table lists some Compound assignment operators:

Operator | Description |
---|---|

`+=` |
`x += 5` equivalent to `x = x + 5` |

`-=` |
`y -= 5` equivalent to `y = y - 5` |

`/=` |
`z /= 3` equivalent to `z = z / 5` |

`%=` |
`m %= 10` equivalent to `m = m % 10` |

The following program demonstrates Compound assignment operators in action:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include<stdio.h> int main(void) { int i = 10; char a = 'd'; printf("ASCII value of %c is %d\n", a, a); // print ASCII value of d a += 10; // increment a by 10; printf("ASCII value of %c is %d\n", a, a); // print ASCII value of n a *= 5; // multiple a by 5; printf("a = %d\n", a); a /= 4; // divide a by 4; printf("a = %d\n", a); a %= 2; // remainder of a % 2; printf("a = %d\n", a); a *= a + i; // is equivalent to a = a * (a + i) printf("a = %d\n", a); return 0; // return 0 to operating system } |

**Expected Output:**

1 2 3 4 5 6 |
ASCII value of d is 100 ASCII value of n is 110 a = 38 a = 9 a = 1 a = 11 |

when we multiply a*=5 why it is equal to 38. explain me. please.

Hi Raida, did you get your answer ?

Hi Raida,

On line no. 14 the expression is (a = a * 5) , where a is 110 from line no. 11.

So right hand side of the above expression evaluate to value 550 and binary conversion of 550 is 0000001000100110. But variable a is of character type and can hold only 1 byte(8 bits).

Taking out 8 bits from 0000001000100110 (traverse from right to left) will results into 38.

Hence the value of a is coming as 38.