Assignment operators

An assignment expression stores a value in the object designated by the left operand. There are two types of assignment operators: The left operand in all assignment expressions must be a modifiable lvalue. The type of the expression is the type of the left operand. The value of the expression is the value of the left operand after the assignment has completed.

The result of an assignment expression is not an lvalue.

All assignment operators have the same precedence and have right-to-left associativity.

Simple assignment operator =

The simple assignment operator has the following form:

lvalue = expr

The operator stores the value of the right operand expr in the object designated by the left operand lvalue.

The left operand must be a modifiable lvalue. The type of an assignment operation is the type of the left operand.

If the left operand is not a class type or a vector type, the right operand is implicitly converted to the type of the left operand. This converted type will not be qualified by const or volatile.

If the left operand is a class type, that type must be complete. The copy assignment operator of the left operand will be called.

If the left operand is an object of reference type, the compiler will assign the value of the right operand to the object denoted by the reference.

IBM extension The assignment operator has been extended to permit operands of vector type. Both sides of an assignment expression must be of the same vector type.

Compound assignment operators

The compound assignment operators consist of a binary operator and the simple assignment operator. They perform the operation of the binary operator on both operands and store the result of that operation into the left operand, which must be a modifiable lvalue.

The following table shows the operand types of compound assignment expressions:

Operator Left operand Right operand
+= or -= Arithmetic Arithmetic
+= or -= Pointer Integral type
*=, /=, and %= Arithmetic Arithmetic
<<=, >>=, &=, ‸=, and |= Integral type Integral type
Note that the expression
a *= b + c
is equivalent to
a = a * (b + c)
and not
a = a * b + c

The following table lists the compound assignment operators and shows an expression using each operator:

Operator Example Equivalent expression
+= index += 2 index = index + 2
-= *pointer -= 1 *pointer = *pointer - 1
*= bonus *= increase bonus = bonus * increase
/= time /= hours time = time / hours
%= allowance %= 1000 allowance = allowance % 1000
<<= result <<= num result = result << num
>>= form >>= 1 form = form >> 1
&= mask &= 2 mask = mask & 2
‸= test ‸= pre_test test = test ‸ pre_test
|= flag |= ON flag = flag | ON

Although the equivalent expression column shows the left operands (from the example column) twice, it is in effect evaluated only once.

IBM extension When GNU C language features have been enabled, compound expressions and conditional expressions are allowed as lvalues, provided that their operands are lvalues. The following compound assignment of the compound expression (a, b) is legal under GNU C, provided that expression b, or more generally, the last expression in the sequence, is an lvalue:
(a,b) += 5  /* Under GNU C, this is equivalent to
a, (b += 5)    */