Operators
A table of operator associativity and precedence
appears in the next section.
Unary operators
Expressions with unary operators
group right to left.
* e-
Indirection operator.
Returns the object or function pointed
to by its operand.
If the type of the expression is ``pointer to ...,''
the type of the result is ``....''
& e-
Address operator.
Returns a pointer to the object or function
referred to by the operand.
Operand must be an lvalue or function type,
and not a bit-field or an object declared register.
Where the operand has type
``type,''
the result has type
``pointer to type.''
- e-
Negation operator.
The operand must have arithmetic type.
Result is the negative of its operand.
Integral promotion is performed
on the operand, and the result has the promoted type.
The negative of an unsigned quantity
is computed by
subtracting its value from
2[n] where n is
the number of bits in the result type.
+ e-
Unary plus operator.
The operand must have arithmetic type.
Result is the value of its operand.
Integral promotion is performed on
the operand, and the result has the
promoted type.
! e-
Logical negation operator.
The operand must have arithmetic or pointer type.
Result is one if the value of its operand is zero,
zero if the value of its operand is nonzero.
The type of the result is
int.
~ e-
The
~
operator yields the one's complement
(all bits inverted) of its operand,
which must have integral type.
Integral promotion is performed on the operand,
and the result has the promoted type.
++e-
The object referred to by the lvalue operand of prefix
++
is incremented.
The value is the new value of the operand
but is not an lvalue.
The expression
++x
is equivalent to
x += 1.
The type of the result is the type of the operand.
--e-
The modifiable lvalue operand of prefix
--
is decremented
analogously to the
prefix
++
operator.
e++-
When postfix
++
is applied to a modifiable lvalue,
the result is the value of the object
referred to by the lvalue.
After the result is noted, the object
is incremented in the same
manner as for the prefix
++
operator.
The type of the result is the same as the type of the lvalue.
e---
When postfix
--
is applied to an lvalue,
the result is the value of the object referred to by the lvalue.
After the result is noted, the object
is decremented in the same manner as for the prefix
--
operator.
The type of the result is the same as the type of the lvalue.
sizeof e-
The
sizeof
operator yields the size
in bytes of its operand.
When applied to an object with array type, the result is the total
number of bytes in the array.
(The size is determined from
the declarations of
the objects in the expression.)
This expression is semantically an
unsigned
constant
(of type size_t, a typedef)
and may be used anywhere a
constant is required
(except in a #if preprocessing directive line).
One major use is in communication with routines
like storage allocators and I/O systems.
sizeof (type)-
The
sizeof
operator may also be applied to a parenthesized type name.
In that case it yields the size in bytes of an object
of the indicated type.
Cast operators - explicit conversions
(type) e-
Placing a parenthesized type name before an expression
converts the value of the expression to that type.
Both the operand and type must be pointer type
or an arithmetic type.
Multiplicative operators
The multiplicative operators
*,
/,
and
%
group left to right.
The usual arithmetic conversions are performed,
and that is the type of the result.
e*e-
Multiplication operator.
The
*
operator is commutative.
e/e-
Division operator.
When positive integers are divided, truncation is toward 0.
If either operand is negative, the quotient is negative.
Operands must be arithmetic types.
e%e-
Remainder operator.
Yields the remainder
from the division of the first expression by the second.
The operands must have integral type.
The sign of the remainder is that of the first operand.
It is always true that
(a/b)*b + a%b
is equal to
a
(if
a/b
is representable).
Additive operators
The additive operators
+
and
-
group left to right.
The usual arithmetic conversions are performed.
There are some additional type possibilities for each operator.
e+e-
Result is the sum of the operands.
A pointer to an object in an array and
an integral value may be added.
The latter is always converted to an address offset
by multiplying it
by the size of the object to which the
pointer points.
The result is a pointer of the same type as the original pointer
that points to another object in the same array,
appropriately offset from the original object.
Thus if
P
is a pointer
to an object in an array, the expression
P+1
is a pointer
to the next object in the array.
No further type combinations are allowed for pointers.
The
+
operator is commutative.
The valid operand type combinations for the
+
operator are:
a + a
p + i or i + p
where
a
is an arithmetic type,
i
is an integral type,
and
p
is a pointer.
e-e-
Result is the difference of the operands.
The operand combinations are the same as for the
+
operator, except that a pointer type may not
be subtracted from an integral type.
Also, if two pointers to objects of the same type are subtracted,
the result is converted
(by division by the size of the object)
to an integer that represents
the number of objects separating
the pointed-to objects.
This conversion will in general give unexpected
results unless the pointers point
to objects in the same array, because pointers, even
to objects of the same type, do not necessarily differ
by a multiple of the object size.
The result type is ptrdiff_t (defined in stddef.h).
ptrdiff_t
is a typedef for
int
in this implementation.
It should be used ``as is'' to ensure portability.
Valid type combinations are
a - a
p - i
p - p
Bitwise shift operators
The bitwise shift operators
<< and >>
take integral operands.
e1 << e2-
Shifts e1 left by e2 bit positions.
Vacated bits are filled with zeros.
e1 >> e2-
Shifts e1 right by e2 bit positions.
Vacated bits are filled with zeros
on the 3B2.
On the Intel386 microprocessor, vacated bits are filled with
zeros if the promoted type of e1 is
an unsigned type.
Otherwise they are filled with copies of the sign
bit of the promoted value of e1.
The result types of the bitwise shift operators are
compilation-mode dependent, as follows:
-Xt-
The result type is
unsigned
if either operand is
unsigned.
-Xa, -Xc-
The result type is the promoted type
of the left operand.
Integral promotion occurs before the shift operation.
Relational operators
a relop a
p relop p
-
The relational operators
<
(less than)
>
(greater than)
<=
(less than or equal to)
>=
(greater than or equal to)
yield 1 if the specified relation is true
and 0 if it is false.
-
The result has type int.
-
Both operands:
-
have arithmetic type; or
-
are pointers to qualified or unqualified
versions of the same object or incomplete types.
Equality operators
a eqop a
p eqop p
p eqop 0
0 eqop p
-
The
==
(equal to)
and
!=
(not equal to)
operators are analogous to the relational operators;
however, they have lower precedence.
Bitwise AND operator
ie1 & ie2
-
Bitwise ``and'' of ie1 and ie2.
-
Value contains a 1 in each bit position
where both ie1 and ie2 contain a 1, and
a 0 in every other position.
-
Operands must be integral;
the usual arithmetic conversions are applied,
and that is the type of the result.
Bitwise exclusive OR operator
ie1 ^ ie2
-
Bitwise exclusive ``or'' of ie1 and ie2.
-
Value contains a 1 in each position where
there is a 1 in either ie1 or ie2, but not both,
and a 0 in every other bit position.
-
Operands must be integral;
the usual arithmetic conversions are applied,
and that is the type of the result.
Bitwise OR operator
ie1 | ie2
-
Bitwise inclusive ``or'' of ie1 and ie2.
-
Value contains a 1 in each bit position
where there is a 1 in either ie1 or ie2,
and a 0 in every other bit position.
-
Operands must be integral;
the usual arithmetic conversions are applied,
and that is the type of the result.
Logical AND operator
e1 && e2
-
Logical ``and'' of e1 and e2.
-
e1 and e2 must be scalars.
-
e1 is evaluated first, and
e2 is evaluated only if e1 is nonzero.
-
Result is 1 if both e1 and e2 are non-zero,
otherwise 0.
-
Result type is int.
Logical OR operator
e1 || e2
-
Logical ``or'' of e1 and e2.
-
e1 and e2 must be scalars.
-
e1 is evaluated first,
and e2 is evaluated only if e1 is zero.
Result is 0 only if both e1 and e2 are false, otherwise 1.
-
Result type is int.
Conditional operator
e ? e1 : e2
-
If e is nonzero, then e1 is evaluated;
otherwise e2 is evaluated.
The value is e1 or e2.
-
The first operand must have scalar type.
-
For the second and third operands, one of the following must be true:
-
Both must be arithmetic types.
The usual arithmetic conversions are performed
to make them a common type and the result has that type.
-
Both must have compatible structure or union type;
the result is that type.
-
Both operands have void type; the result has void type.
-
Both operands are pointers to qualified or unqualified
versions of compatible types.
The result type is the composite type.
-
One operand is a pointer and the other is a null pointer constant.
The result type is the pointer type.
-
One operand is a pointer to an object or incomplete type and
the other is a pointer to a qualified or unqualified version of
void.
The result type is a pointer to void.
For the pointer cases (the last three), the result is
a pointer to a type qualified by all the qualifiers
of the types pointed to by the operands.
Assignment expressions
-
Assignment operators are:
= *= /= %= += -= <<= >>= &= |= ^=
-
An expression of the form e1 op= e2
is equivalent to e1 = e1 op (e2)
except that e1 is evaluated only once.
-
The left operand:
-
must be a modifiable lvalue.
-
must have arithmetic type,
or, for += and -=,
must be a pointer to an object type and
the right operand must have integral type.
-
of an = operator,
if the operand is a structure or union,
must not have any member
or submember qualified with const.
-
Result type is the type of the (unpromoted) left operand.
Comma operator
e1 , e2
-
e1 is evaluated first, then e2.
-
The result has the type and value of
e2
and is not an lvalue.
Structure operators
su.mem
Indicates member mem of structure or union su.
sup -> mem
Indicates member mem of structure or union
pointed to by sup.
Equivalent to (*sup).mem.
Next topic:
Associativity and precedence of operators
Previous topic:
Primary expressions
© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004