precedence of binary operators

A

arnuld

/* C++ Primer - 4/e

* chapter 5 - Expressions

* exercises 5.1 and 5.2
*/


#include <iostream>

int main()
{
std::cout << "I) -30 / 3 * 21 % 4: "
<< -30 / 3 * 21 % 4
<< "\n\n"
<< "II) -30 + 3 * 21 / 5: "
<< -30 + 3 * 21 / 5
<< "\n\n"
<< "III) -30 * 3 + 21 / 5: "
<< -30 * 3 + 21 /5
<< "\n\n"
<< "IV) 3 * 21 % 4: "
<< 3 * 21 % 4
<< "\n\n"
<< "V) 12 / 3 * 4 + 5 * 15 + 24 % 4 / 2: "
<< 12 / 3 * 4 + 5 * 15 + 24 % 4 / 2
<< std::endl;

return 0;
}

========== OUTPUT =============
~/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra ex_05-01.cpp
~/programming/cpp $ ./a.out
I) -30 / 3 * 21 % 4: -2

II) -30 + 3 * 21 / 5: -18

III) -30 * 3 + 21 / 5: -86

IV) 3 * 21 % 4: 3

V) 12 / 3 * 4 + 5 * 15 + 24 % 4 / 2: 91 ~/programming/cpp $


i understood how the 4th expressions worked, (3 * (21 % 4)), which shows
that % has higher precedence than *. 2nd expression works like: (-30 + 3
* (21 / 5)) which shows that / has higher precedence than *. i am not able
to understand the default grouping of other expressions. take the 1st
expresson:

((-30 / 3) * (21 % 4)), (-30 / ((3 * 21) % 4), (-30 / (3 * (21 % 4))

but nothing gives output of -2. same for other expressions. can anybody
point me where i am wrong ?
 
A

arnuld

Really? What about this result makes you think so?

ok , see:

(3 * (21 % 4)) -> (3 * 1) -> 3

Ouch!

(3 * 21) % 4) -> (63 % 4) -> 3

Aye...... which one is true ?
 
A

arnuld

ok , see:

(3 * (21 % 4)) -> (3 * 1) -> 3

Ouch!

(3 * 21) % 4) -> (63 % 4) -> 3

Aye...... which one is true ?


i think 2nd one is true and precedence goes like this:

* / % + -

therefore, -30 / 3 * 21 % 4 must output this:

((-30 / (3 * 21)) % 4) -> ((-30 / 63) % 4) -> (0 % 4) -> 0

but it does not :(
 
J

Jim Langston

arnuld said:
i think 2nd one is true and precedence goes like this:

* / % + -

therefore, -30 / 3 * 21 % 4 must output this:

((-30 / (3 * 21)) % 4) -> ((-30 / 63) % 4) -> (0 % 4) -> 0

but it does not :(

went to google.com. typed in "C++ operator precedence" hit return. First
llink is titled "C++ Operator Precedence".
http://www.cppreference.com/operator_precedence.html

After looking at that comeback if you still have questions.
 
V

Victor Bazarov

arnuld said:
[..] does comp.lang.c++ folks really recommenend to trust
some online C++ resourece ? (except C++ FAQs, of course)

No. But since 'comp.lang.c++' is also an "online C++ resource",
could you trust *us* to dispense opinions about what to trust?
Makes you think, don't it?
 
M

Marcus Kwok

arnuld said:
i think 2nd one is true and precedence goes like this:

* / % + -

Almost, it is like

(* / %) (+ -)

where * / % have the same precedence, but are evaluated from left to
right.
therefore, -30 / 3 * 21 % 4 must output this:

((-30 / (3 * 21)) % 4) -> ((-30 / 63) % 4) -> (0 % 4) -> 0

(((-30 / 3) * 21) % 4) -> ((-10 * 21) % 4) -> (-210 % 4) -> -2
 
J

James Kanze

went to google.com. typed in "C++ operator precedence" hit return. First
llink is titled "C++ Operator Precedence".http://www.cppreference.com/operator_precedence.html

Just a nit, because it doesn't apply here, but formally, C++
expressions aren't defined in terms of precedence, but in terms
of the grammar itself. And while it is generally easier (and
useful) to think of most of the operators as having a
precedence, there are a few particular cases where precedence
can't describe what is happening. (The ?: is one. A mix of
unary operators and the C style cast expression is another.)
 
J

Jim Langston

went to google.com. typed in "C++ operator precedence" hit return.
First
llink is titled "C++ Operator
Precedence".http://www.cppreference.com/operator_precedence.html

Just a nit, because it doesn't apply here, but formally, C++
expressions aren't defined in terms of precedence, but in terms
of the grammar itself. And while it is generally easier (and
useful) to think of most of the operators as having a
precedence, there are a few particular cases where precedence
can't describe what is happening. (The ?: is one. A mix of
unary operators and the C style cast expression is another.)

Okay, what is wrong with how it is desicrbed in the link I gave?
wrong with how it is desicrbed in the link I gave?

Operator: ? :
Description: Ternary conditional (if-then-else)
Example: int i = (a > b) ? a : b;
Associativity: right to left

Does that not describe it? Or is there something else beyond this? I
seldom use the Ternary operator and when I do I only use one in a statment.
Usually simple things like:
std::string Describe = (Value == 0) ? "None" : StrmConvert( Value );
or such which I don't have to worry about the order. Doesn't the "right to
left" describe it well?
 
J

James Kanze

"James Kanze" <[email protected]> wrote in message
Just a nit, because it doesn't apply here, but formally, C++
expressions aren't defined in terms of precedence, but in terms
of the grammar itself. And while it is generally easier (and
useful) to think of most of the operators as having a
precedence, there are a few particular cases where precedence
can't describe what is happening. (The ?: is one. A mix of
unary operators and the C style cast expression is another.)
Okay, what is wrong with how it is desicrbed in the link I gave?
wrong with how it is desicrbed in the link I gave?
Operator: ? :
Description: Ternary conditional (if-then-else)
Example: int i = (a > b) ? a : b;
Associativity: right to left
Does that not describe it? Or is there something else beyond this?

It says it has higher precedance than assignment. If you write:

x = c ? a : b ;

this is true, but if you write:

c ? x = a : y ;

then it will be evaluated before the assignment.

That's what I meant when I said that you can't totally describe
it with precedence alone. If you look in the grammar, you'll
find that the production for ?: uses assignment-expression, and
the production for assignment-expression uses
conditional-expression. There's a cycle.
I
seldom use the Ternary operator and when I do I only use one in a statment.
Usually simple things like:
std::string Describe = (Value == 0) ? "None" : StrmConvert( Value );
or such which I don't have to worry about the order.

The problem isn't the order, but the precedence with respect to
other operators. And if I called it a nit, it's because you
shouldn't be writing code complicated enough that it matters:).
The precedences on the page you give are a good first
approximation, and should be sufficient for all, or at least
almost all well written code. Even if it's formally wrong in a
few exotic cases.
 
A

Andrew Koenig

Just a nit, because it doesn't apply here, but formally, C++
expressions aren't defined in terms of precedence, but in terms
of the grammar itself. And while it is generally easier (and
useful) to think of most of the operators as having a
precedence, there are a few particular cases where precedence
can't describe what is happening. (The ?: is one. A mix of
unary operators and the C style cast expression is another.)

Actually, precedence can describe ?:, by saying that every ? must bind to a
corresponding :, with the effect that ? acts like ?( and : acts like ):, and
the precedence of the whole shebang is the same as that of the assignment
operators, and is right-associative like the assignment operators.

I know that most people don't describe it this way, but I once analyzed the
grammar and I am convinced that it works.
It says it has higher precedance than assignment. If you write:

x = c ? a : b ;
this is true, but if you write:

c ? x = a : y ;
then it will be evaluated before the assignment.

Every ? binds to a :, so we rewrite the two examples:

x = c ?( a ): b
c ?( x = a ): y

and the problem vanishes.

The first example above is the interesting one; because ?: has the same
precedence as = and is right-associative, it means the same as

x = (c ? a : b)

I think that if you look at other examples, you will still see that
precedence works here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top