Assignment Operators

Operators Description
= Simple Assignment

The simple assignment operator stores the value of the right operand in the object designated by the left operand.

Both operands must have arithmetic types, the same structure type, or the same union type. Otherwise, both operands must be pointers to the same type, or the left operand must be a pointer and the right operand must be the constant 0 or NULL. If the language level is extended, both operands can be pointers to different types.

If both operands have arithmetic types, the system converts the type of the right operand to the type of the left operand before the assignment.

If the left operand is a pointer and the right operand is the constant 0, the result is NULL.

Pointers to void can appear on either side of the simple assignment operator.

A packed structure or union can be assigned to a nonpacked structure or union of the same type, and a nonpacked structure or union can be assigned to a packed structure or union of the same type.

If one operand is packed and the other is not, the layout of the right operand is remapped to match the layout of the left. This remapping of structures might degrade performance. For efficiency, when you perform assignment operations with structures or unions, you should ensure that both operands are either packed or nonpacked.

Note: If you assign pointers to structures or unions, the objects they point to must both be either packed or nonpacked.

You can assign values to operands with the type qualifier volatile. You cannot assign a pointer of an object with the type qualifier const to a pointer of an object without the const type qualifier. For example:

const int *p1;
int *p2;
p2 = p1;  /* this is NOT allowed */

p1 = p2;  /* this IS allowed */

Note: The assignment (=) operator should not be confused with the equality operator (==).

For example,

if(x == 3)
evaluates to 1 if x is equal to three. Equality tests like this should be coded with spaces between the operator and the operands to prevent unintentional assignments.
if(x = 3)
is taken to be true because (x = 3) evaluates to a non-zero value (3). The expression also assigns the value 3 to x.
+=
-=
*=
/=
%=
<<=
>>=
&=
^=
|=
Compound Assignment

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 give the result of that operation to the left operand.

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.

 



Operator Precedence and Associativity
Expressions and Operators
Types of Expressions


Operator Precedence and Associativity Table
Primary Operators
Unary Operators
Binary Operators
Conditional Operator
Comma Operator
Examples Using Compound Assignment Operators