-
C Reference Manual - 7
7.4.1 expression + expression
The result is the sum of the expressions. If both operands are int or char, the result is int. If both are float
or double, the result is double. If one is char or int and one is float or double, the former is converted to
double and the result is double. If an int or char is added to a pointer, the former is converted by multiplying
it by the length of the object to which the pointer points and the result is a pointer of the same type as the original
pointer. Thus if P is a pointer to an object, the expression ‘‘P+1’’ is a pointer to another object of the same type as
the first and immediately following it in storage.
No other type combinations are allowed.
7.4.2 expression − expression
The result is the difference of the operands. If both operands are int, char, float,ordouble, the same type
considerations as for + apply. If an int or char is subtracted from a pointer, the former is converted in the same
way as explained under + above.
If two pointers to objects of the same type are subtracted, the result is converted (by division by the length of the
object) to an int representing the number of objects separating the pointed-to objects. This conversion will in gen-
eral give unexpected results unless the pointers point to objects in the same array, since pointers, even to objects of
the same type, do not necessarily differ by a multiple of the object-length.
7.5 Shift operators
The shift operators << and >> group left-to-right.
7.5.1 expression << expression
7.5.2 expression >> expression
Both operands must be int or char, and the result is int. The second operand should be non-negative. The
value of ‘‘E1<<E2’’ is E1 (interpreted as a bit pattern 16 bits long) left-shifted E2 bits; vacated bits are 0-filled. The
value of ‘‘E1>>E2’’ is E1 (interpreted as a two’s complement, 16-bit quantity) arithmetically right-shifted E2 bit po-
sitions. Vacated bits are filled by a copy of the sign bit of E1. [Note: the use of arithmetic rather than logical shift
does not survive transportation between machines.]
7.6 Relational operators
The relational operators group left-to-right, but this fact is not very useful; ‘‘a<b<c’’ does not mean what it seems
to.
7.6.1 expression < expression
7.6.2 expression > expression
7.6.3 expression <= expression
7.6.4 expression >= expression
The operators < (less than), > (greater than), <= (less than or equal to) and >= (greater than or equal to) all yield 0
if the specified relation is false and 1 if it is true. Operand conversion is exactly the same as for the + operator ex-
cept that pointers of any kind may be compared; the result in this case depends on the relative locations in storage of
the pointed-to objects. It does not seem to be very meaningful to compare pointers with integers other than 0.
7.7 Equality operators
7.7.1 expression == expression
7.7.2 expression != expression
The == (equal to) and the != (not equal to) operators are exactly analogous to the relational operators except for
their lower precedence. (Thus ‘‘a<b == c<d’’ is 1 whenever a<b and c<d have the same truth-value).
7.8 expression & expression
The & operator groups left-to-right. Both operands must be int or char; the result is an int which is the bit-
wise logical and function of the operands.