473,414 Members | 1,937 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,414 software developers and data experts.

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 2619
pu*******@blueyonder.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*******@blueyonder.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*******@blueyonder.co.uk> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.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
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...
8
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...
13
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...
4
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....
21
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
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...
32
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...
54
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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,...
0
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...

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.