473,606 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Order of evaluation

Is the following statement true?

"It is required that the operands to an operator be fully evaluated
before the operator itself is evaluated."

The only way I can think of that it would be practically possible to
violate this would be for subexpression like a += b to return its value
(a + b) for use in the parent expression, but leave it until some time
later before it assigned that value to a. Is this allowed?

If this statement above is true, then the following is defined:

int a = /*something*/, b = /*something else*/;
a ^= b ^= a ^= b;

A couple of people at this page:
http://c2.com/cgi/wiki?ThreeStarProgrammer, have said that the above
expression is undefined because it modifies 'a' twice without an
intervening sequence point. If operands have to be fully evaluated
before the operator can be evaluated, this is not undefined though,
because it is equivalent to (a ^= (b ^= (a ^= b))), in which each
operator with side-effects depends on the result of the next. Is it
undefined or not?

ps. Clearly the order of evaluation of the subexpressions in (a ^= b)
^= (a ^= b) is undefined - I'm certainly not disagreeing with that.

pps. If a and b were of a user-defined type, the ^= operator would be a
function call, and the parameters to a function call have to be
evaluated before the call, so in this case it would be defined.

Jul 23 '05 #1
7 2634
pu*******@bluey onder.co.uk wrote:
Is the following statement true?

"It is required that the operands to an operator be fully evaluated
before the operator itself is evaluated."
Yes.
The only way I can think of that it would be practically possible to
violate this would be for subexpression like a += b to return its value
(a + b) for use in the parent expression, but leave it until some time
later before it assigned that value to a. Is this allowed?
Yes, and it sometimes causes trouble if 'a' is changed twice before the
next sequence point.
If this statement above is true, then the following is defined:

int a = /*something*/, b = /*something else*/;
a ^= b ^= a ^= b;
Nope. This expression changes the value of 'a' and 'b' twice between the
sequence points.
A couple of people at this page:
http://c2.com/cgi/wiki?ThreeStarProgrammer, have said that the above
expression is undefined because it modifies 'a' twice without an
intervening sequence point. If operands have to be fully evaluated
before the operator can be evaluated, this is not undefined though,
because it is equivalent to (a ^= (b ^= (a ^= b))), in which each
operator with side-effects depends on the result of the next. Is it
undefined or not?
It is undefined. Evaluation of operands (obtaining the values) and side
effects taking place are two different things.
ps. Clearly the order of evaluation of the subexpressions in (a ^= b)
^= (a ^= b) is undefined - I'm certainly not disagreeing with that.

pps. If a and b were of a user-defined type, the ^= operator would be a
function call, and the parameters to a function call have to be
evaluated before the call, so in this case it would be defined.


Yes.

V
Jul 23 '05 #2
> ... Evaluation of operands (obtaining the values) and side
effects taking place are two different things.


It all comes clear to me now. Thanks very much for your help.

Jul 23 '05 #3
Just to make sure I've got this straight:
int a = 1, b = 2, c = 3;
(a = b) = c;
This is undefined too, isn't it? 'a' could end up containing either 2
or 3 as there are no sequence points between the two assignments.

Thanks,
Tom

Jul 23 '05 #4
pu*******@bluey onder.co.uk wrote:
Just to make sure I've got this straight:
int a = 1, b = 2, c = 3;
(a = b) = c;
This is undefined too, isn't it? 'a' could end up containing either 2
or 3 as there are no sequence points between the two assignments.


That is a good question. Let me start the explanation by saying that
you're *incorrect* using the word "either" here. _If_ the behaviour is
undefined, *anything* can happen.

Now, to the actual thing. I have read the Standard (again) and can only
say that it's ambiguous at best. The exact text is "The result of the
assignment operation is the value stored in the left operand after the
assignment has taken place; the result is an lvalue." How should we
understand this? Maybe my bad English plays tricks on me, but I read
that sentence as dual in meaning. OOH, it can mean, "The result of the
assignment operation is only known after assignment is complete (and the
value is stored)". OTOH, it can mean, "The result of the assignment
operation is _the same_ as what _will be stored_ when the sequence point
is reached". If it's the former, then both expressions (the one you asked
about before and the one you're asking about here) are well-defined, since
assignment always takes place before the lvalue is returned. If it is the
latter that is true, then both forms are undefined.

I believe the safer would be interpret it as the latter. I am now unsure,
though, which one is the correct interpretation.

V
Jul 23 '05 #5
That is confusing, indeed. It is good advice to assume it is unsafe and
just avoid such things. I'm getting the feeling I should get a copy of
the standard, as well.
Thanks again for your help. Your English doesn't seem bad at all.
Tom

Jul 23 '05 #6

<pu*******@blue yonder.co.uk> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
Is the following statement true?

"It is required that the operands to an operator be fully evaluated
before the operator itself is evaluated."


It is true in all except two cases: a && b and a || b. In the first case,
if a evaluates to false then a &&b must be false whatever the value of b is.
Similarly, in the second case if a evaluates to true then a || b must be
true whatever b is. In both these cases, the operator immediately returns
the correct value after the first operand is evaluated, and the second one
isn't evaluated at all. This is called "short-circuit evaluation".

Joe Gottman
Jul 23 '05 #7
Joe Gottman wrote:

It is true in all except two cases: a && b and a || b.


The ternary operator is also an exception.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #8

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

Similar topics

16
697
by: Bhushit Joshipura | last post by:
This post contains one question and one proposal. A. May I know why order of evaluation of arguments is not specified in C/C++? I asked a question in comp.lang.c++ for the following possibility and because the languages do not specify the order of evaluation, doing so was an error. int B::f ( int i, int j = i + 1 ) { // j defaults to i + 1
8
3339
by: der | last post by:
Hello all, I've a question about order of evaluations in expressions that have && and || operators in them. The question is: will the evalution go left-to-right, no matter what -- even if the || operator is before the && operator? e,g, In an expression like a = (z>x) || (x<0) && (z-y>9); is it guaranteed that z>x will be checked first?
13
2651
by: Richard | last post by:
Boy, I'll sure bet this is a FAQ. Many years ago, my "runtime behavior of programming languages" prof absolutely guaranteed that C parameters are evaluated left-to-right. He was a bright guy from CMU whose research focus was on programming languages, and I believed him, but now I'm not so sure. Little help?
4
2570
by: Frank Wallingford | last post by:
Note: For those with instant reactions, this is NOT the common "why is i = i++ not defined?" question. Please read on. I came across an interesting question when talking with my colleagues. According to the standard, most operators evaluate their operands in an unspecified order. This means that in code like this: f() + g()
21
4103
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
9
2500
by: John Smith | last post by:
I've been playing with splint, which returns the following warning for the code below: statlib.c: (in function log_norm_pdf) statlib.c(1054,31): Expression has undefined behavior (left operand uses errno, modified by right operand): (log(x) - mu) * (log(x) - mu) Code has unspecified behavior. Order of evaluation of function parameters or subexpressions is not defined, so if a value is used and
32
3290
by: silpau | last post by:
hi, i am a bit confused on expression evaluation order in expressions involving unary increment.decrement operators along with binary operators. For example in the following expression x += i + j + k++;
54
3904
by: Rasjid | last post by:
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. 1) "The order of evaluation of subexpressions is determined by the precedence and grouping of operators."
0
8010
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8429
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8084
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6761
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5963
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5461
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3969
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1550
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.