473,320 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

C/C++ Ambiguity in Order of Evaluation

Hello,

I have just joined and this is my first post.

I have never been able to resolve the issue of order of evaluation in
C/C++ and the related issue of precedence of operators, use of
parentheses.
>From the reference manual I have :-
1) "The order of evaluation of subexpressions is determined by the
precedence and grouping of operators."
2) order of evaluation of operands of operators undefined except where
noted.

I was just told at another forum that
v = u - ((u & -(u = 1)) << 1);
has undefined behavior and the reason given was
the order of evaluation of operands u, -(u = 1) of & is undefined.
That unary - has the highest precedence or the use of parentheses in -
(u = 1) is of no consequence.

Also all over the internet, the impression I get about
x = a * b + c;
is that the order of evaluation is a * b, ? + c, x = ?
But the order(undefined) of evaluation may be (c) , a * b,
x = ? + ?

The same person seem to say unequivocally (?) that precedence
of operators has nothing to do with order of evaluation, but only
about binding operands to operators.

So I have the following questions :-
1) v = u - ((u & -(u = 1)) << 1); Is this undefined and why?
2) Does precedence of operators determine order of evaluation ? If
Yes, then in what situations.
3) If answer to 2) is No, then how is C affected if precedence of
operators is redefined in sort of this manner :-
"precedence of operators determines the grouping of operands with
operators" - without any reference to order of evaluation.

Best Regards,
Rasjid

Jun 20 '07
54 3839
On Jun 20, 11:14 pm, Richard Heathfield <r...@see.sig.invalidwrote:
Rasjid wrote:
2) Does precedence of operators determine order of evaluation ? If
Yes, then in what situations.

No, it doesn't, except accidentally. For example, it's hard to come up
with an order of evaluation for x * y + z that doesn't do the
multiplication before it does the addition.
An RPN-style evaluator might push z, then push x and y,
then mult, then add.

Jun 24 '07 #51
Old Wolf <ol*****@inspire.net.nzwrites:
On Jun 20, 11:14 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>Rasjid wrote:
2) Does precedence of operators determine order of evaluation ? If
Yes, then in what situations.

No, it doesn't, except accidentally. For example, it's hard to come up
with an order of evaluation for x * y + z that doesn't do the
multiplication before it does the addition.

An RPN-style evaluator might push z, then push x and y,
then mult, then add.
Which *does* do the multiplication before it does the addition.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 25 '07 #52
On Jun 25, 12:40 pm, Keith Thompson <k...@mib.orgwrote:
Richard Heathfield <r...@see.sig.invalidwrote:
No, it doesn't, except accidentally. For example, it's hard to come up
with an order of evaluation for x * y + z that doesn't do the
multiplication before it does the addition.

Which *does* do the multiplication before it does the addition.
Sorry, I took R.H.'s comment to read: evaluates
the arguments of the multiplication before
evaluating those of the addition.

Jun 25 '07 #53
On Jun 22, 12:21 pm, Keith Thompson <k...@mib.orgwrote:
If your reply is just plain text, you don't need a preview.
I used to use Google Groups preview feature to
check that my lines were short enough so that
others didn't see a long-short-long-short wrapping
effect when reading my posts. Since they removed
that option, I just have to cross my fingers..:)

Jun 25 '07 #54
In article <fP******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>For example, it's hard to come up
with an order of evaluation for x * y + z that doesn't do the
multiplication before it does the addition.
It might use a multiply-and-add instruction which does them both at
once.

(I assume multiply-and-add works domething like this: to do 12*23+45
it does 10*23 + 2*23 + 45, so that the multiplication might at least
not be finished before the addition.)

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jun 25 '07 #55

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Clay_Culver | last post by:
My professor claims that this code: int f() { int x = 5; int &y = x; x += ++y; return x; }
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.