HP 3000 Manuals

Assignment Operators [ HP C/iX Reference Manual ] MPE/iX 5.0 Documentation


HP C/iX Reference Manual

Assignment Operators 

Assignment operators assign the value of the right operand to the object
designated by the left operand.

Syntax 

     assignment-expression ::=
         conditional-expression 
         unary-expression assignment-operator assignment-expression 

     assignment-operator ::= one selected from the set
         = *= /= %= += -= <<= >>= &=  ^= |=

Description 

Each assignment operator must have a modifiable lvalue as its left
operand.  An assignment operator stores a value into the left operand.
The C language does not define the order of evaluation of the left and
right operands.  For this reason, you should avoid operations with side
effects (such as ++ or --) if their operands appear on both the left and
right side of the assignment.  For example, you should not write an
expression like the following because the results depend on which operand
is evaluated first.

     *p++ = *p--

Simple Assignment 

In simple assignment, the value of the right operand replaces the value
of the object specified by the left operand.  If the source and
destination objects overlap storage areas, the results of the assignment
are undefined.

The left and right operands can be any of the following combinations:

   1.  Both arithmetic 

       If both of the operands are arithmetic types, the type of the
       right operand is converted to the type of the left operand.  The
       converted value is then stored in the location specified by the
       left operand.

   2.  Both structure/union 

       If both operands are structures or unions of the same type, the
       right structure/union is copied into the left structure/union.  A
       union is considered to be the size of the largest member of the
       union, and it is this number of bytes that is moved.

   3.  Left operand is a pointer to type T 

       In this case, the right operand can also be a pointer to type T.
       The right operand is then copied to the left operand.

       The right operand can also be a null pointer constant or a pointer
       to void.

       A special case of pointer assignment involves the assignment of a
       pointer to void to another pointer.  No cast is necessary to
       convert a "pointer to void" to any other type of pointer.

An assignment is not only an operation, it is also an expression.  Each
operand must have an arithmetic type consistent with those allowed by the
binary operator that is used to make up the assignment operator.  You can
use the += and -= operators with a left operand that is a pointer type.

Compound Assignment 

Given the general assignment operator op=, if used in the expression

       A op= B

the result is equal to the following assignment

       A = A op (B)

except that the expression represented by A is evaluated only once.

Therefore,

       A[f()] += B

is very different from

       A[f()] = A[f()] + B

because the latter statement causes the function f() to be invoked twice.

Assignment operators are useful to reference complex subscript operators.
For example:

       a[j+2/i] += 3.5

In this case, the subscript expression is evaluated only once.

Examples 

     a += 5            Add 5 to a. 
     a *= 2            Multiply a by 2. 
     a = b             Assign b to a. 
     a <<= 1           Left shift a by 1 bit 



MPE/iX 5.0 Documentation