Assignment Operators
Assignments are performed by simple and compound assignment operators, as shown in Table 1-10.
|
Operator |
Meaning |
Example |
Result |
= |
Simple assignment |
x = y |
Assign the value of |
op= |
Compound assignment |
x += y |
|
The left operand in an assignment must be an
lvalue; i. e., an expression that designates an
object. This object is assigned a new value.
The simplest examples of lvalues are variable
names. In the case of a pointer variable ptr, both
ptr and *ptr are
lvalues. Constants and expressions such as
x+1, on the other hand, are not
lvalues.
The following operands are permissible in a simple
assignment (=):
Two operands with arithmetic types
Two operands with the same structure or union type
Two pointers that both point to objects of the same type, unless the right operand is the constant
NULL
If one operand is a pointer to an object, then the other may be a
pointer to the “incomplete” type
void (i. e., void *).
If the two operands have different types, the value of the right operand is converted to the type of the left operand.
An assignment expression has the type and value of the left operand after the assignment. Assignments are grouped from right to left. For example:
a = b = 100; // equivalent to a=(b=100);
// The value 100 is assigned to b and a.A compound assignment has the form x
op= y, where op is a ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access