HP C A.06.05 Reference Manual

Expressions and Operators
Assignment Operators (=, +=, -=, *=, /=, %=,<<=, >>=, &=, ^=, |=)
Chapter 588
x = 3; /* assigns the value 3 to variable x */
x = y; /* assigns the value of y to x */
x = (y*z); /* performs the multiplication and */
/* assigns the result to x */
An assignment expression itself has a value, which is the same value that is assigned to the
left operand.
The assignment operator has right-to-left associativity, so the expression
a = b = c = d = 1;
is interpreted as
(a = (b = (c = (d = 1))));
First 1 is assigned to d, then d is assigned to c, then c is assigned to b, and finally, b is
assigned to a. The value of the entire expression is 1. This is a convenient syntax for assigning
the same value to more than one variable. However, each assignment may cause quiet
conversions, so that
int j;
double f;
f = j = 3.5;
assigns the truncated value 3 to both f and j. Conversely,
j = f = 3.5;
assigns 3.5 to f and 3 to j.
The Other Assignment Operators
C's assignment operators provide a handy way to avoid some keystrokes. Any statement in
which the left side of the equation is repeated on the right is a candidate for an assignment
operator. If you have a statement like this:
i = i + 10;
you can use the assignment operator format to shorten the statement to
i += 10;
In other words, any statement of the form
var = var op exp; /* traditional form */
can be represented in the following shorthand form:
var op = exp; /* shorthand form */
where var is a variable, op is a binary operator, and exp is an expression.