By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,930 Members | 1,422 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,930 IT Pros & Developers. It's quick & easy.

Question about postfix ++ precedence

P: n/a
Hello,

Could someone explain to me why the postfix ++ operator is listed near
the top of the operator precedence (above even prefix ++ on my book's
chart) but is not evaluated before operators of lower precedence? It
seems like a contradiction to me.

Thanks,
Glen

Feb 7 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
mr********@gmail.com wrote:
Could someone explain to me why the postfix ++ operator is listed near
the top of the operator precedence (above even prefix ++ on my book's
chart) but is not evaluated before operators of lower precedence? It
seems like a contradiction to me.


Precedence controls the order of application. Postfix has side effect
(changing the value of the operand) which is only observable at the
next sequence point. The result of the postfix increment or decrement
is the value of the operand _before_ it's applied. No contradiction.

V
--
Please remove capital As from my address when replying by mail
Feb 7 '06 #2

P: n/a
mr********@gmail.com wrote:
Could someone explain to me why the postfix ++ operator is listed near
the top of the operator precedence (above even prefix ++ on my book's
chart) but is not evaluated before operators of lower precedence? It
seems like a contradiction to me.


Because precedence and order-of-evaluation are unrelated.

Feb 7 '06 #3

P: n/a
> Because precedence and order-of-evaluation are unrelated.

Ok... what's the difference, then?

Glen

Feb 7 '06 #4

P: n/a
mr********@gmail.com wrote:
Because precedence and order-of-evaluation are unrelated.


Ok... what's the difference, then?

Glen


If you don't concern yourself with such things like overflow,
precedence determines the logic, while order of execution is
at the liberty of the optimizer.

unsigned a, b, c; // no overflow
...
unsigned d = a + b - c;

'+' and '-' have the same precedence and while they are usually
grouped from left to right, the compiler is free to evaluate the
above expression as if it were written

unsigned d = a + (b - c);

or

unsigned d = (a - c) + b;

In that context we say that the order of execution is unspecified.

That's how I see it anyway. A proper book on on computer science
must explain it better.

V
--
Please remove capital As from my address when replying by mail
Feb 7 '06 #5

P: n/a
On 6 Feb 2006 17:21:02 -0800, mr********@gmail.com wrote:
Hello,

Could someone explain to me why the postfix ++ operator is listed near
the top of the operator precedence (above even prefix ++ on my book's
chart) but is not evaluated before operators of lower precedence? It
seems like a contradiction to me.

Thanks,
Glen


With an example:

a=b+c++;

Operator precedence tells the compiler to interpret that line as

a= b+ (c++); // YES

and not something like

a=(b+c)++; // NO

But order of evaluation is unspecified. The expression could be
evaluated as:

Either:
-- calculate b+c
-- increment c
-- store calculation in a

Or:

-- calculate b+c
-- store calculation in a
-- increment c

whichever fits better the compiler, depending on lots of things, such
as optimization level, surrounding operations... Same compiler may
compile the expression in many different ways, as long as the
calculation is correct.

Regards,

Zara
Feb 7 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.