HP C A.06.05 Reference Manual
Expressions and Operators
Assignment Operators (=, +=, -=, *=, /=, %=,<<=, >>=, &=, ^=, |=)
Chapter 5 89
The only internal difference between the two forms is that var is evaluated only once in the
shorthand form. Most of the time this is not important; however, it is important when the left
operand has side effects, as in the following example:
int *ip;
*ip++ += 1; /* These two statements produce */
*ip++ = *ip++ + 1; /* different results. */
The second statement is ambiguous because C does not specify which assignment operand is
evaluated first. See “Operator Precedence” on page 135 for more information concerning order
of evaluation.
Assignment Type Conversions
Whenever you assign a value to a variable, the value is converted to the variable's data type if
possible. In the example below, for instance, the floating-point constant 3.5 is converted to an
int so that i gets the integer value 3.
int i;
i = 3.5;
Integer to Character Conversions Unlike arithmetic conversions, which always expand
the expression, assignment conversions can truncate the expression and therefore affect its
value. For example, suppose c is a char, and you make the following assignment:
c = 882;
The binary representation of 882 is
00000011 01110010
This number requires two bytes of storage, but the variable c has only one byte allocated for
it, so the two upper bits don't get assigned to c. This is known as overflow, and the result is
not defined by the ANSI/ISO C standard or the K&R language definition for signed types. HP
C simply ignores the extra byte, so c would be assigned the rightmost byte:
01110010
This would erroneously give c the value of 114. The principle illustrated for chars also applies
to shorts, ints, and long ints. For unsigned types, however, C has well-defined rules for
dealing with overflow conditions. When an integer value x is converted to a smaller unsigned
integer type, the result is the non-negative remainder of
x / (U_MAX+1)
where U_MAX is the largest number that can be represented in the shorter unsigned type. For
example, if j is an unsigned short, which is two bytes, then the assignment
j = 71124;