DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
C language compiler

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 non-zero. 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(TM) 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:

-Xk, -Xt
The result type is unsigned if either operand is unsigned.

-Xa, -Xc, -Xm
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

Equality operators

   a eqop a
   p eqop p
   p eqop 0
   0 eqop p

Bitwise AND operator

   ie1 & ie2

Bitwise exclusive OR operator

   ie1 ^ ie2

Bitwise OR operator

   ie1 | ie2

Logical AND operator

   e1 && e2

Logical OR operator

   e1 || e2

Conditional operator

   e ? e1 : e2

Assignment expressions

Comma operator

   e1 , e2

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

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003