Binary Operators

Operators Description
* Multiplication

The * (multiplication) operator yields the product of its operands. The operands must have an arithmetic type. The result is not an lvalue. The usual arithmetic conversions on the operands are performed.

Because the multiplication operator has both associative and commutative properties, the compiler can rearrange the operands in an expression that contains more than one multiplication operator. For example, the expression:

sites * number * cost

can be interpreted in any of the following ways:

(sites * number) * cost
sites * (number * cost)
(cost * sites) * number
/ Division

The / (division) operator yields the quotient of its operands. The operands must have an arithmetic type. The result is not an lvalue.

If both operands are positive integers and the operation produces a remainder, the remainder is ignored. For example, expression 7 / 4 yields the value 1 (rather than 1.75 or 2).

On all IBM C compilers, if either operand is negative, the result is rounded towards zero.

The result is undefined if the second operand evaluates to 0.

For more information on generating warning messages for division by constant zero, see the info compiler option on AIX, or the /Wgroup option on Intel.

The usual arithmetic conversions on the operands are performed.

% Remainder

The % (remainder) operator yields the remainder from the division of the left operand by the right operand. For example, the expression 5 % 3 yields 2. The result is not an lvalue.

Both operands must have an integral type. If the right operand evaluates to 0, the result is undefined. If either operand has a negative value, the result is such that the following expression always yields the value of a if b is not 0 and a / b is representable:

( a / b ) * b + a % b;

The sign of the remainder is the same as the sign of the quotient.

The usual arithmetic conversions on the operands are performed.

+ Addition

The + (addition) operator yields the sum of its operands. Both operands must have an arithmetic type, or one operand must be a pointer to an object type and the other operand must have an integral type.

When both operands have an arithmetic type, the usual arithmetic conversions on the operands are performed. The result has the type produced by the conversions on the operands and is not an lvalue.

A pointer to an object in an array can be added to a value having integral type. The result is a pointer of the same type as the pointer operand. The result refers to another element in the array, offset from the original element by the amount specified by the integral value. If the resulting pointer points to storage outside the array, other than the first location outside the array, the result is undefined. The compiler does not provide boundary checking on the pointers. For example, after the addition, ptr points to the third element of the array:

int array[5];
int *ptr;
ptr = array + 2;
- Subtraction

The - (subtraction) operator yields the difference of its operands. Both operands must have an arithmetic type, or the left operand must have a pointer type and the right operand must have the same pointer type or an integral type. You cannot subtract a pointer from an integral value.

When both operands have an arithmetic type, the usual arithmetic conversions on the operands are performed. The result has the type produced by the conversions on the operands and is not an lvalue.

When the left operand is a pointer and the right operand has an integral type, the compiler converts the value of the right to an address offset. The result is a pointer of the same type as the pointer operand.

If both operands are pointers to the same type, the compiler converts the result to an integral type that represents the number of objects separating the two addresses. Behavior is undefined if the pointers do not refer to objects in the same array.

<<
>>
Bitwise Shifts

The bitwise shift operators move the bit values of a binary object. The left operand specifies the value to be shifted. The right operand specifies the number of positions that the bits in the value are to be shifted. The result is not an lvalue. Both operands have the same precedence and are left-to-right associative.

Operator Usage
<< Indicates the bits are to be shifted to the left.
>> Indicates the bits are to be shifted to the right.

Each operand must have an integral type. The compiler performs integral promotions on the operands. Then the right operand is converted to type int. The result has the same type as the left operand (after the arithmetic conversions).

The right operand should not have a negative value or a value that is greater than or equal to the width in bits of the expression being shifted. The result of bitwise shifts on such values is unpredictable.

If the right operand has the value 0, the result is the value of the left operand (after the usual arithmetic conversions).

The << operator fills vacated bits with zeros. For example, if left_op has the value 4019, the bit pattern (in 32-bit format) of left_op is:

00000000000000000000111110110011

The expression left_op << 3 yields:

00000000000000000111110110011000

The following table shows the behavior of the >> operator:

Left Operand Type Result of >>
unsigned type The vacated bits are filled with zeros.
Nonnegative unsigned type The integral part of the quotient of the left operand divided by the quantity 2, raised to the power of the right operand. The vacated bits of a signed value are filled with a copy of the sign bit of the unshifted value.
Negative signed type The language does not specify how the vacated bits produced by the >> operator are filled.
<
>
<=
>=
Relational

The relational operators compare two operands and determine the validity of a relationship. If the relationship stated by the operator is true, the value of the result is 1. If false, the value of the result is 0. The result is not an lvalue.

The following table describes the four relational operators:

Operator Usage
< Indicates whether the value of the left operand is less than the value of the right operand.
> Indicates whether the value of the left operand is greater than the value of the right operand.
<= Indicates whether the value of the left operand is less than or equal to the value of the right operand.
>= Indicates whether the value of the left operand is greater than or equal to the value of the right operand.

Both operands must have arithmetic types or be pointers to the same type. The result has type int.

If the operands have arithmetic types, the usual arithmetic conversions on the operands are performed.

When the operands are pointers, the result is determined by the locations of the objects to which the pointers refer. If the pointers do not refer to objects in the same array, the result is not defined.

Relational operators have left-to-right associativity. For example, the expression:

a < b <= c

is interpreted as:

(a < b) <= c

If the value of a is less than the value of b, the first relationship is true and yields the value 1. The compiler then compares the value 1 with the value of c.

==
!=
Equality

The equality operators, like the relational operators, compare two operands for the validity of a relationship. The equality operators, however, have a lower precedence than the relational operators. If the relationship stated by an equality operator is true, the value of the result is 1. Otherwise, the value of the result is 0.

The following table describes the two equality operators:

Operator Usage
== Indicates whether the value of the left operand is equal to the value of the right operand.
!= Indicates whether the value of the left operand is not equal to the value of the right operand.

Both operands must have arithmetic types or be pointers to the same type, or one operand must have a pointer type and the other operand must be a pointer to void or NULL. The result has type int.

If the operands have arithmetic types, the usual arithmetic conversions on the operands are performed.

If the operands are pointers, the result is determined by the locations of the objects to which the pointers refer.

If one operand is a pointer and the other operand is an integer having the value 0, the == expression is true only if the pointer operand evaluates to NULL. The != operator evaluates to true if the pointer operand does not evaluate to NULL.

You can also use the equality operators to compare pointers to members that are of the same type but do not belong to the same object. The following expressions contain examples of equality and relational operators:

time < max_time == status < complete
letter != EOF

Note: The equality operator (==) should not be confused with the assignment (=) operator.

For example,

if(x == 3)
evaluates to 1 if x is equal to three. Equality tests like this should be coded with spaces between the operator and the operands to prevent unintentional assignments.
if(x = 3)
is taken to be true because (x = 3) evaluates to a non-zero value (3). The expression also assigns the value 3 to x.
& Bitwise AND

The & (bitwise AND) operator compares each bit of its first operand to the corresponding bit of the second operand. If both bits are 1's, the corresponding bit of the result is set to 1. Otherwise, it sets the corresponding result bit to 0.

Both operands must have an integral type. The usual arithmetic conversions on each operand are performed. The result has the same type as the converted operands.

Because the bitwise AND operator has both associative and commutative properties, the compiler can rearrange the operands in an expression that contains more than one bitwise AND operator.

The following example shows the values of a, b, and the result of a & b represented as 32-bit binary numbers:

bit pattern of a 00000000000000000000000001011100
bit pattern of b 00000000000000000000000000101110
bit pattern of a & b 00000000000000000000000000001100

Note: The bitwise AND (&) should not be confused with the logical AND (&&) operator. For example, 1 & 4 evaluates to 0 while 1 && 4 evaluates to 1

^ Bitwise Exclusive OR

The bitwise exclusive OR operator compares each bit of its first operand to the corresponding bit of the second operand. If both bits are 1's or both bits are 0's, the corresponding bit of the result is set to 0. Otherwise, it sets the corresponding result bit to 1.

Both operands must have an integral type. The usual arithmetic conversions on each operand are performed. The result has the same type as the converted operands and is not an lvalue.

Because the bitwise exclusive OR operator has both associative and commutative properties, the compiler can rearrange the operands in an expression that contains more than one bitwise exclusive OR operator even when the sub-expressions are explicitly grouped with parentheses.

The following example shows the values of a, b, and the result of a ^ b represented as 32-bit binary numbers:

bit pattern of a 00000000000000000000000001011100
bit pattern of b 00000000000000000000000000101110
bit pattern of a ^ b 00000000000000000000000001110010
| Bitwise Inclusive OR

The | (bitwise inclusive OR) operator compares the values (in binary format) of each operand and yields a value whose bit pattern shows which bits in either of the operands has the value 1. If both of the bits are 0, the result of that bit is 0; otherwise, the result is 1.

Both operands must have an integral type. The usual arithmetic conversions on each operand are performed. The result has the same type as the converted operands and is not an lvalue.

Because the bitwise inclusive OR operator has both associative and commutative properties, the compiler can rearrange the operands in an expression that contains more than one bitwise inclusive OR operator even when the subexpressions are explicitly grouped with parentheses.

The following example shows the values of a, b, and the result of a | b represented as 32-bit binary numbers:

bit pattern of a 00000000000000000000000001011100
bit pattern of b 00000000000000000000000000101110
bit pattern of a | b 00000000000000000000000001111110

Note: The bitwise OR (|) should not be confused with the logical OR (||) operator. For example, 1 | 4 evaluates to 5 while 1 || 4 evaluates to 1

&& Logical AND

The && (logical AND) operator indicates whether both operands have a nonzero value. If both operands have nonzero values, the result has the value 1. Otherwise, the result has the value 0.

Both operands must have a scalar type. The usual arithmetic conversions on each operand are performed. The result has type int and is not an lvalue.

Unlike the & (bitwise AND) operator, the && operator guarantees left-to-right evaluation of the operands. If the left operand evaluates to 0, the right operand is not evaluated.

The following examples show how the expressions that contain the logical AND operator are evaluated:

Expression Result
1 && 0 0
1 && 4 1
0 && 0 0

The following example uses the logical AND operator to avoid division by zero:

(y != 0) && (x / y)

The expression x / y is not evaluated when y != 0 evaluates to 0.

Note: The logical AND (&&) should not be confused with the bitwise AND (&) operator. For example: 1 && 4 evaluates to 1 while 1 && 4 evaluates to 0

|| Logical OR

The || (logical OR) operator indicates whether either operand has a nonzero value. If either operand has a nonzero value, the result has the value 1. Otherwise, the result has the value 0.

Both operands must have a scalar type. The usual arithmetic conversions on each operand are performed. The result has type int and is not an lvalue.

Unlike the | (bitwise inclusive OR) operator, the || operator guarantees left-to-right evaluation of the operands. If the left operand has a nonzero value, the right operand is not evaluated.

The following examples show how expressions that contain the logical OR operator are evaluated:

Expression Result
1 || 0 1
1 || 4 1
0 || 0 0

The following example uses the logical OR operator to conditionally increment y:

++x || ++y;

The expression ++y is not evaluated when the expression ++x evaluates to a nonzero quantity.

Note: The logical OR ( || ) should not be confused with the bitwise OR ( | ) operator. For example: 1 || 4 evaluates to 1 while 1 | 4 evaluates to 5

 



Operator Precedence and Associativity
Expressions and Operators
Types of Expressions
Arithmetic Conversions
Standard Type Conversions
Pointer Conversions


Operator Precedence and Associativity Table
Primary Operators
Unary Operators
Conditional Operator
Assignment Operators
Comma Operator
Arithmetic Conversions
Pointer Arithmetic