Saturday, July 9, 2011

Operator Precedence in C

The operators at the top of this list are evaluated first.

It is important to note that there is no specified precedence for the operation of changing a variable into a value. For example, consider the following code:

float x, result;
x = 1;
result = x / ++x;

The value of result is not guaranteed to be consistent across different compilers, because it is not clear whether the computer should change the variable x (the one that occurs on the left side of the division operator) before using it. Depending on which compiler you are using, the variable result can either be 1.0 or 0.5. The bottom line is that you should not use the same variable multiple times in a single expression when using operators with side effects.
 
Precedence
Operator
Description
Example
Associativity
1 :: Scoping operator Class::age = 2; none
2 ()
[]
->
.
++
--
Grouping operator
Array access
Member access from a pointer
Member access from an object
Post-increment
Post-decrement
(a + b) / 4;
array[4] = 2;
ptr->age = 34;
obj.age = 34;
for( i = 0; i < 10; i++ ) ...
for( i = 10; i > 0; i-- ) ...
left to right
3 !
~
++
--
-
+
*
&
(type)
sizeof
Logical negation
Bitwise complement
Pre-increment
Pre-decrement
Unary minus
Unary plus
Dereference
Address of
Cast to a given type
Return size in bytes
if( !done ) ...
flags = ~flags;
for( i = 0; i < 10; ++i ) ...
for( i = 10; i > 0; --i ) ...
int i = -1;
int i = +1;
data = *ptr;
address = &obj;
int i = (int) floatNum;
int size = sizeof(floatNum);
right to left
4 ->*
.*
Member pointer selector
Member object selector
ptr->*var = 24;
obj.*var = 24;
left to right
5 *
/
%
Multiplication
Division
Modulus
int i = 2 * 4;
float f = 10 / 3;
int rem = 4 % 3;
left to right
6 +
-
Addition
Subtraction
int i = 2 + 3;
int i = 5 - 1;
left to right
7 <<
>>
Bitwise shift left
Bitwise shift right
int flags = 33 << 1;
int flags = 33 >> 1;
left to right
8 <
<=
>
>=
Comparison less-than
Comparison less-than-or-equal-to
Comparison greater-than
Comparison geater-than-or-equal-to
if( i < 42 ) ...
if( i <= 42 ) ...
if( i > 42 ) ...
if( i >= 42 ) ...
left to right
9 ==
!=
Comparison equal-to
Comparison not-equal-to
if( i == 42 ) ...
if( i != 42 ) ...
left to right
10 & Bitwise AND flags = flags & 42; left to right
11 ^ Bitwise exclusive OR flags = flags ^ 42; left to right
12 | Bitwise inclusive (normal) OR flags = flags | 42; left to right
13 && Logical AND if( conditionA && conditionB ) ... left to right
14 || Logical OR if( conditionA || conditionB ) ... left to right
15 ? : Ternary conditional (if-then-else) int i = (a > b) ? a : b; right to left
16 =
+=
-=
*=
/=
%=
&=
^=
|=
<<=
>>=
Assignment operator
Increment and assign
Decrement and assign
Multiply and assign
Divide and assign
Modulo and assign
Bitwise AND and assign
Bitwise exclusive OR and assign
Bitwise inclusive (normal) OR and assign
Bitwise shift left and assign
Bitwise shift right and assign
int a = b;
a += 3;
b -= 4;
a *= 5;
a /= 2;
a %= 3;
flags &= new_flags;
flags ^= new_flags;
flags |= new_flags;
flags <<= 2;
flags >>= 2;
right to left
17 , Sequential evaluation operator for( i = 0, j = 0; i < 10; i++, j++ ) ... left to right

You may have noticed that in most of the macro definition examples shown above, each occurrence of a macro argument name had parentheses around it. In addition, another pair of parentheses usually surround the entire macro definition. Here is why it is best to write macros that way,

Suppose you define a macro as follows, 

#define ceil_div(x, y) (x + y - 1) / y

whose purpose is to divide, rounding up. (One use for this operation is to compute how many int objects are needed to hold a certain number of char objects.) Then suppose it is used as follows:

a = ceil_div (b & c, sizeof (int));
==> a = (b & c + sizeof (int) - 1) / sizeof (int);

This does not do what is intended. The operator-precedence rules of C make it equivalent to this:

a = (b & (c + sizeof (int) - 1)) / sizeof (int);

What we want is this:

a = ((b & c) + sizeof (int) - 1)) / sizeof (int);

Defining the macro as

#define ceil_div(x, y) ((x) + (y) - 1) / (y)

provides the desired result.

Unintended grouping can result in another way. Consider sizeof ceil_div(1, 2). That has the appearance of a C expression that would compute the size of the type of ceil_div (1, 2), but in fact it means something very different. Here is what it expands to: 

sizeof ((1) + (2) - 1) / (2)

This would take the size of an integer and divide it by two. The precedence rules have put the division outside the sizeof when it was intended to be inside. Parentheses around the entire macro definition prevent such problems. Here, then, is the recommended way to define ceil_div:

#define ceil_div(x, y) (((x) + (y) - 1) / (y))

No comments:

Post a Comment