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

Order of parameter evaulation

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?

--
Bush is blowing it.
Nov 13 '05 #1
13 2629
Richard <rh***@hotmail.com> wrote:
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?


You're right, this is in the FAQ.

http://www.eskimo.com/~scs/C-faq/top.html

Or, more specifically:

http://www.eskimo.com/~scs/C-faq/q3.4.html

Alex
Nov 13 '05 #2
Richard wrote:
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.


It's good not to be so sure: either he's wrong or your understanding of
what he said is wrong.
--
Martin Ambuhl

Nov 13 '05 #3
Richard wrote:

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?


http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/

N869
3.19
[#1] unspecified behavior
behavior where this International Standard provides two or
more possibilities and imposes no requirements on which is
chosen in any instance

[#2] EXAMPLE An example of unspecified behavior is the
order in which the arguments to a function are evaluated.
Nov 13 '05 #4
al*******@hotmail.com wrote...
Richard <rh***@hotmail.com> wrote:
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?


You're right, this is in the FAQ.

http://www.eskimo.com/~scs/C-faq/top.html

Or, more specifically:

http://www.eskimo.com/~scs/C-faq/q3.4.html


Thanks--I saw that. I was thinking more like, for example,

if ( cond1 && (cond2 || cond3) ) {...}

is there a predictable order of parameter evaluation? My experience
is that this is undefined and compiler-specific.

I'm really sure about what he said, because we talked about it a few
times afterwards in the context of using the behavior in production
programs.

--
Bush is blowing it.
Nov 13 '05 #5
pf*****@mindspring.com wrote...
Richard wrote:

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?


http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/

N869
3.19
[#1] unspecified behavior
behavior where this International Standard provides two or
more possibilities and imposes no requirements on which is
chosen in any instance

[#2] EXAMPLE An example of unspecified behavior is the
order in which the arguments to a function are evaluated.


Perfect. Thank you very much. (My prof was right about everything
else!)

--
Bush is blowing it.
Nov 13 '05 #6
In article <MP************************@news.verizon.net>,
Richard <rh***@hotmail.com> wrote:
N869
3.19
[#1] unspecified behavior
behavior where this International Standard provides two or
more possibilities and imposes no requirements on which is
chosen in any instance

[#2] EXAMPLE An example of unspecified behavior is the
order in which the arguments to a function are evaluated.


Perfect. Thank you very much. (My prof was right about everything
else!)


But in another post, you ask about:
if (cond1 || cond2) {
[ ... ]
}
In that case, cond1 is guaranteed to be completely evaluated before
cond2. (And cond2 is guaranteed to be evaluated if and only if cond1
is zero.)

-- Brett
Nov 13 '05 #7
Richard <rh***@hotmail.com> wrote:
al*******@hotmail.com wrote...
Richard <rh***@hotmail.com> wrote:
> 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?


You're right, this is in the FAQ.

http://www.eskimo.com/~scs/C-faq/top.html

Or, more specifically:

http://www.eskimo.com/~scs/C-faq/q3.4.html


Thanks--I saw that. I was thinking more like, for example,

if ( cond1 && (cond2 || cond3) ) {...}

is there a predictable order of parameter evaluation? My experience
is that this is undefined and compiler-specific.


The && and || operators are something of a special case because they
(like the , and ?: operators) introduce additional sequence points. In
the expression:

a && (b || c)

Then a is completely evaluated first, and all side-effects happen. If a
was zero, then the result of the complete expression is zero and no
further evaluation happens. Otherwise, b is completely evaluated and
all side-effects take place - if b was non-zero, the result of the
complete expression is 1 and no further evaluation happens. Otherwise,
c is evaluated and the result of the complete expression is zero if c
was zero, otherwise it is 1. Any side-effects caused by evaluating c do
not necessarily take effect until the next sequence point.

On the other hand, in:

a & (b | c)

....then all of a, b and c are evaluated (unless the compiler can prove
that no visible side-effects result from some evaluation), in an
unspecified order. Any side-effects do not necessarily take effect
until the next sequence point.

- Kevin.

Nov 13 '05 #8
Brett Frankenberger wrote:

In article <MP************************@news.verizon.net>,
Richard <rh***@hotmail.com> wrote:
N869
3.19
[#1] unspecified behavior
behavior where this International Standard provides two or
more possibilities and imposes no requirements on which is
chosen in any instance

[#2] EXAMPLE An example of unspecified behavior is the
order in which the arguments to a function are evaluated.


Perfect. Thank you very much. (My prof was right about everything
else!)


But in another post, you ask about:
if (cond1 || cond2) {
[ ... ]
}
In that case, cond1 is guaranteed to be completely evaluated before
cond2. (And cond2 is guaranteed to be evaluated if and only if cond1
is zero.)


.... which has nothing to do with parameters.
Parameters are the local objects in functions
which get initialized to the argument values in a function call

--
pete
Nov 13 '05 #9
Richard <rh***@hotmail.com> writes:
Richard <rh***@hotmail.com> wrote:
Many years ago, my "runtime behavior of programming languages" prof
absolutely guaranteed that C parameters are evaluated left-to-right.

Wrong. There is no such guarantee. In fact, I suspect that many
implementations always evaluate parameters from right to left.
Thanks--I saw that. I was thinking more like, for example,

if ( cond1 && (cond2 || cond3) ) {...}

is there a predictable order of parameter evaluation?


There aren't any parameters there, just operands. Functions have
parameters (the expression supplied as a parameter value is
called an argument), whereas operators have operands. && and ||
are special--they always evaluate their left operands first, and
then the right operand only if necessary. Most other operators
do not share this guarantee.
--
"It wouldn't be a new C standard if it didn't give a
new meaning to the word `static'."
--Peter Seebach on C99
Nov 13 '05 #10
Richard wrote:

Thanks--I saw that. I was thinking more like, for example,

if ( cond1 && (cond2 || cond3) ) {...}

is there a predictable order of parameter evaluation? My experience
is that this is undefined and compiler-specific.


I was right: you misunderstood what was being said. There are *no*
parameters here, so parameter evaluation order is a red herring. So, you
see, it helps to ask the question in concrete terms (as here) rather than
in abstract terms (as before, when you used the wrong ones).
The order of evaluation of logical expressions is well-defined; in
particular, '&&' and '||' have left-to-right evaluation.

--
Martin Ambuhl

Nov 13 '05 #11
Richard <rh***@hotmail.com> wrote:
al*******@hotmail.com wrote...
Richard <rh***@hotmail.com> wrote:
> 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?


You're right, this is in the FAQ.

http://www.eskimo.com/~scs/C-faq/top.html

Or, more specifically:

http://www.eskimo.com/~scs/C-faq/q3.4.html

Thanks--I saw that. I was thinking more like, for example, if ( cond1 && (cond2 || cond3) ) {...} is there a predictable order of parameter evaluation? My experience
is that this is undefined and compiler-specific.


There are no parameters in your example.

Moreover, this is a special case due to C's short-circuit evaluation
in logical operators. If 'cond1' is false then there is no reason to
evaluate anything after the &&. If 'cond2' is true then there is no
reason to evaluate 'cond3'. Therefore, in this particular instance,
you are guaranteed left-to-right evaluation.

The evaluation order is somewhat dictated by 'sequence points'. The
end of a full expression is a sequence point, so are the '&&', '||',
'?', and ',' operators. In the absence of a sequence point, the
evaluation order is unspecified.

The following will be evaluated as written:

a();
b();
c();

This may not be:

int i = a() + b() + c();

Alex
Nov 13 '05 #12
Alex <al*******@hotmail.com> writes:
Richard <rh***@hotmail.com> wrote:
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?


You're right, this is in the FAQ.

http://www.eskimo.com/~scs/C-faq/top.html

Or, more specifically:

http://www.eskimo.com/~scs/C-faq/q3.4.html


Hmm. q3.4 talks about the order of evaluation of operands, but
doesn't directly refer to order of evaluation of function arguments.
In fact, I don't see anything in the FAQ that does.

I can imagine a hypothetical C-like language in which operands are
evaluated in an unspecified order, but function arguments are always
evaluated left-to-right. As far as I can tell, the C FAQ would be as
correct for this hypothetical language as it is for C.

Which is an annoyingly roundabout way of saying that the FAQ doesn't
seem to cover this point explicitly. (And it probably should.)

(But it turns out the OP wasn't talking about function arguments
anyway; if he'd asked the right question, he could have found the
answer in the FAQ.)

--
Keith Thompson (The_Other_Keith) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #13
Keith Thompson <ks*@cts.com> wrote:
Alex <al*******@hotmail.com> writes:
Richard <rh***@hotmail.com> wrote:
> 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?


You're right, this is in the FAQ.

http://www.eskimo.com/~scs/C-faq/top.html

Or, more specifically:

http://www.eskimo.com/~scs/C-faq/q3.4.html

Hmm. q3.4 talks about the order of evaluation of operands, but
doesn't directly refer to order of evaluation of function arguments.
In fact, I don't see anything in the FAQ that does.


You're right, of course. I missed the word 'parameters' when I read
the original post. However, it seems that the OP did the same thing :-).

Alex

Nov 13 '05 #14

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

Similar topics

2
by: One's Too Many | last post by:
Ran into a strange problem today: 8.1.7 on AIX 4.3.3 Database and applications had been working fine for two years and all of a sudden a couple of regularly-run queries are now no longer...
4
by: dave | last post by:
hi all, hope someone can help.... i'm having trouble calling an SP where the ORDER BY operator is specified as a parameter when the SP is called my SP is..... CREATE PROCEDURE...
6
by: lig | last post by:
hi , i am trying to do something that is not working (cuz i tried that) so i hope for some ideas about it. ok, so i have this recursive function, lets call it recFun and i wanted to be able to...
1
by: Hrvoje Vrbanc | last post by:
Hello all! I would be grateful if someone could offer me some help with the following problem: I have a DataGrid control with sort enabled. Therefore it could be sorted in several different...
3
by: Jesper Jensen | last post by:
I have the following problem: I have created the following SQL for my app. With the below shown code (Example 1) I am able to retrieve the records I need into dataset dsFind. Now however I want...
1
by: Thomas Schoen | last post by:
Hi, is it possible to use a parameter of a plpgsql-function to order a selection inside the function? What i would like to do is pass a column-name/alias-name to a plpgsql function and use...
3
by: Beowulf | last post by:
I was just messing around with some ad hoc views and table returning UDFs today so I could look at and print out data from a small table and noticed something strange. If I stick my select...
11
by: Israel | last post by:
I've trying to write a query that seems like it should be simple but for some reason my attempts are not working. This is really a general SQL quesion and doesn't pertain to MySQL but I couldn't...
3
by: altafur | last post by:
hi, i am using java with hibernate . i want to run a hibernate order by query thru java. i have written the query in hibernate.hbm.xml file. the query is as follows: query: SELECT distinct...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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...

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.