# Assignment operators

< c‎ | language

Assignment and compound assignment operators are binary operators that modify the variable to their left using the value to their right.

Operator Operator name Example Description Equivalent of
= basic assignment a = b a becomes equal to b N/A
+= addition assignment a += b a becomes equal to the addition of a and b a = a + b
-= subtraction assignment a -= b a becomes equal to the subtraction of b from a a = a - b
*= multiplication assignment a *= b a becomes equal to the product of a and b a = a * b
/= division assignment a /= b a becomes equal to the division of a by b a = a / b
%= modulo assignment a %= b a becomes equal to the remainder of a divided by b a = a % b
&= bitwise AND assignment a &= b a becomes equal to the bitwise AND of a and b a = a & b
|= bitwise OR assignment a |= b a becomes equal to the bitwise OR of a and b a = a | b
^= bitwise XOR assignment a ^= b a becomes equal to the bitwise XOR of a and b a = a ^ b
<<= bitwise left shift assignment a <<= b a becomes equal to a left shifted by b a = a << b
>>= bitwise right shift assignment a >>= b a becomes equal to a right shifted by b a = a >> b

### Simple assignment

The simple assignment operator expressions have the form

 lhs = rhs

where

 lhs - modifiable lvalue expression of any complete object type rhs - expression of any type implicitly convertible to lhs or compatible with lhs

Assignment performs implicit conversion from the value of rhs to the type of rhs and then replaces the value in the object designated by lhs with the converted value of rhs.

Assignment also returns the same value as what was stored in lhs (so that expressions such as a = b = c are possible). The value category of the assignment operator is non-lvalue (so that expressions such as (a=b)=c are invalid).

rhs and lhs must satisfy one of the following:

• both lhs and rhs have arithmetic types, in which case lhs may be volatile-qualified or atomic
• both lhs and rhs have pointer to compatible (ignoring qualifiers) types, or one of the pointers is a pointer to void, and the conversion would not add qualifiers to the pointed-to type. lhs may be volatile or restrict-qualified or atomic.
• lhs is a pointer (possibly qualified or atomic) and rhs is a null pointer constant such as NULL
• lhs has type _Bool (possibly qualified or atomic) and rhs is a pointer