views:

237

answers:

8

Referring to the O'Reilly pocket reference for C, I'm a little confused by the description for grouping of the *, /, and % operators. The book says that grouping occurs left to right -- now I think I'm getting grouping confused with evaluation order. Given the following equation, and the rules established from the book, I would have thought that...

int x = 4 / 3 * -3

... evaluates to 0, because...

1: 4 / 3 * -3
2: 4 / -9
3: 0

... however, it actually evaluates to -3, and it seems to use this method...

1: 4 / 3 * -3
2: 1 * -3
3: -3

Why is that?

+6  A: 

It makes sense to me:

int x = 4 / 3 * -3;

Grouping left to right, we get:

int x = (4 / 3) * -3
int x = ((4 / 3) * -3);

Also see the precedence table. They are at the same precedence, so they bind left to right.

Matthew Flaschen
Ah, I read it such that it chooses the left-most operator first in the list of `*`, `/`, `%` and groups this first, rather than the left most operator in the equation. Thanks for the clarification.
nbolton
A: 

These links should help you out.

LeopardSkinPillBoxHat
A: 

Multiply and divide are left associative, so the second order is what happens - the operation is grouped as (4/3), then the result is multiplied by -3.

dsolimano
A: 

For math, C works just like you learned in high scool. Remember BODMAS (Brackets of Division, multiplication, addition and subtraction). This means it looks for a calculation from the left to the right. In this case it sees 4/3 and calculates the answer, then multiplies the answer with -3. You can use brackets to fix that (4/(3*-3)). Have a look at this page for a summary of how C orders operators and performs the calculations.

Marius
we learned PEMDAS: parentheses, exponents, multiplication, division, addition, subtraction (it also sounded much cooler colloquially - "pem-dos")
advs89
+1  A: 

Here , it is left associative for system recognisation . So , it will execute second example only for evaluating the expression.

pavun_cool
+1  A: 

IMHO, it is good to know about these operator precedences, but it is better to use parentheses whenever there is doubt :-). As the masters say, the code is more for human readers than for the machine; if the author is not sure, nor will be the readers.

ArunSaha
+1 for massive use of parentheses :)
Johan
+2  A: 

Dear friend,

Visit the following URL. It is very useful all the topics in C. So you can use the operator precedence also.

 http://www.goldfish.org/books/The%20C%20Programming%20Language%20-%20K&R/chapter2.html#s2.12
muruga
So friendly! :)
LiraNuna
+4  A: 

You need to know both the precedence and the associativity of operators.

Multiplication (*) has higher precedence than addition (+), which is why 2+3*4 is interpreted as 2+(3*4), both in C and normal math. But in an expression like 2*3/4, or 2*3*4, the operators all have the same precedence, and you need to look at the associativity. For most operators, it is left to right, which means that you start grouping from the left: 2*3/4 becomes (2*3)/4, 2*3*4*5 becomes ((2*3)*4)*5, and so on.

An exception is assignment, which is an operator in C. Assignment is right associative, so a=b=3 should be read as a=(b=3).

Any good C book or tutorial should have a table of all the operators (such as this one), with both precedence and associativity.

Thomas Padron-McCarthy