473,320 Members | 1,876 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.

Arg Eval

Could someone give me the name of a compiler/environment where this code
[below] evaluates left to right (or, at least not right to left!) - as in
the compiler evaluates the parameter
list left to right. This is for a c programming class, and all the gcc
implentations I have to hand does this right to left. So, I'd like to give
a concrete counter example.

#include <stdio.h>

int main(void)
{
int n = 1;

/* They'd like this to result in 1, 10, 100.
*/
printf("%d %d %d", n, n *= 10, n *= 10);

return 0;
}

Nov 15 '05 #1
15 1370
rayw <ra*********@gmail.com> wrote:
#include <stdio.h> int main(void)
{
int n = 1; /* They'd like this to result in 1, 10, 100.
*/
If "they" are your instructors, drop the class immediately and tell
them to learn

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

before they presume to teach a class on C.
printf("%d %d %d", n, n *= 10, n *= 10);
This is Bad Code and you can by no means expect it to produce reliable
results even if the compiler/implementation tells you in what order it
evaluates the function arguments (and it might not, as the Standard
permits it to do).
return 0;
}


--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #2

"rayw" <ra*********@gmail.com> wrote in message
news:dj**********@news.ox.ac.uk...
Could someone give me the name of a compiler/environment where this code
[below] evaluates left to right (or, at least not right to left!) - as in
the compiler evaluates the parameter
list left to right. This is for a c programming class,
If your instructor is telling you to write code
that depends upon that, you need a new instructor.
and all the gcc
implentations I have to hand does this right to left. So, I'd like to
give
a concrete counter example.
The language standard does not specify order of
evaluation of function arguments; therefore one
should not write code that depends upon it.

#include <stdio.h>

int main(void)
{
int n = 1;

/* They'd like this to result in 1, 10, 100.
*/
printf("%d %d %d", n, n *= 10, n *= 10);

return 0;


printf("%d %d %d", n, n * 10, n * 10 * 10);

or

printf("%d %d %d", n, n * 10, n * 100);

This will always produce:
1 10 100

.... no matter the compiler.

-Mike

Nov 15 '05 #3
rayw wrote:
Could someone give me the name of a compiler/environment where this code
[below] evaluates left to right (or, at least not right to left!) - as in
the compiler evaluates the parameter
list left to right.
Not here, or at least not reliably, since the order of evaluation of
parameters in unspecified according to the standard, so the compiler
could produce code that evaluates them in a different order on each run.
This is for a c programming class, and all the gcc
implentations I have to hand does this right to left. So, I'd like to give
a concrete counter example.
If you were asked to do this in class then it was a singularly pointless
assignment.
#include <stdio.h>

int main(void)
{
int n = 1;

/* They'd like this to result in 1, 10, 100.
*/
printf("%d %d %d", n, n *= 10, n *= 10);
This invokes undefined behaviour, which means that literally anything
can happen.
1) n is modified twice without an intervening sequence point which is
undefined behaviour
2) n is used for a purpose other than calculating the new value of n
which I believe also invokes undefined behaviour.

I can certainly real ways it could produce 10 10 10 as output.

This is all strongly related to stuff in the comp.lang.c FAQ, most
explicitly http://www.eskimo.com/~scs/C-faq/q3.2.html
return 0;
}

--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #4
On Fri, 21 Oct 2005 23:18:43 +0100, "rayw" <ra*********@gmail.com>
wrote:
Could someone give me the name of a compiler/environment where this code
[below] evaluates left to right (or, at least not right to left!) - as in
the compiler evaluates the parameter
list left to right. This is for a c programming class, and all the gcc
implentations I have to hand does this right to left. So, I'd like to give
a concrete counter example.

#include <stdio.h>

int main(void)
{
int n = 1;

/* They'd like this to result in 1, 10, 100.
*/
printf("%d %d %d", n, n *= 10, n *= 10);
Since this code invokes undefined behavior, you are on a wild goose
chase from the beginning.

return 0;
}

<<Remove the del for email>>
Nov 15 '05 #5

Flash Gordon wrote:
rayw wrote:
#include <stdio.h>

int main(void)
{
int n = 1;

/* They'd like this to result in 1, 10, 100.
*/
printf("%d %d %d", n, n *= 10, n *= 10);
This invokes undefined behaviour, which means that literally anything
can happen.
1) n is modified twice without an intervening sequence point which is
undefined behaviour


Isn't the comma operator a sequence point? Moreover, isn't the passing
each argument a sequence point in of itself?
2) n is used for a purpose other than calculating the new value of n
which I believe also invokes undefined behaviour.


Only if it is in the same expression.

Nov 15 '05 #6
Razzer wrote:
Flash Gordon wrote:
rayw wrote:

#include <stdio.h>

int main(void)
{
int n = 1;

/* They'd like this to result in 1, 10, 100.
*/
printf("%d %d %d", n, n *= 10, n *= 10);
This invokes undefined behaviour, which means that literally anything
can happen.
1) n is modified twice without an intervening sequence point which is
undefined behaviour

Isn't the comma operator a sequence point? Moreover, isn't the passing
each argument a sequence point in of itself?

The comma operator is indeed a sequence point. The comma(s) in an
argument list, however, are *not* comma operators. Different animal.

[snip]

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Nov 15 '05 #7

Artie Gold wrote:
Razzer wrote:
Flash Gordon wrote:
rayw wrote:
#include <stdio.h>

int main(void)
{
int n = 1;

/* They'd like this to result in 1, 10, 100.
*/
printf("%d %d %d", n, n *= 10, n *= 10);

This invokes undefined behaviour, which means that literally anything
can happen.
1) n is modified twice without an intervening sequence point which is
undefined behaviour

Isn't the comma operator a sequence point? Moreover, isn't the passing
each argument a sequence point in of itself?

The comma operator is indeed a sequence point. The comma(s) in an
argument list, however, are *not* comma operators. Different animal.


You're right. I was trying to make an analogy between the two, although
I should've been more explicit.

[snip]

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"


Nov 15 '05 #8

"Razzer" <co********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...

Flash Gordon wrote:
rayw wrote:
> #include <stdio.h>
>
> int main(void)
> {
> int n = 1;
>
> /* They'd like this to result in 1, 10, 100.
> */
> printf("%d %d %d", n, n *= 10, n *= 10);
This invokes undefined behaviour, which means that literally anything
can happen.
1) n is modified twice without an intervening sequence point which is
undefined behaviour


Isn't the comma operator a sequence point?


There are no occurrences of the comma operator in that code.
Moreover, isn't the passing
each argument a sequence point in of itself?


No.

-Mike
Nov 15 '05 #9
Razzer wrote:
Flash Gordon wrote:
rayw wrote:
printf("%d %d %d", n, n *= 10, n *= 10);
This invokes undefined behaviour, which means that literally anything
can happen.
1) n is modified twice without an intervening sequence point which is
undefined behaviour
Isn't the comma operator a sequence point? Moreover, isn't the passing
each argument a sequence point in of itself?


There is no comma operator anywhere in that statement, so whether a
comma operator marks a sequence point is irrelevant.
Nov 15 '05 #10
>> > #include <stdio.h>
>
> int main(void)
> {
> int n = 1;
>
> /* They'd like this to result in 1, 10, 100.
> */
> printf("%d %d %d", n, n *= 10, n *= 10);
This invokes undefined behaviour, which means that literally anything
can happen.
1) n is modified twice without an intervening sequence point which is
undefined behaviour


Isn't the comma operator a sequence point?


There are no comma operators in the above code.
Moreover, isn't the passing
each argument a sequence point in of itself?


No.

Gordon L. Burditt
Nov 15 '05 #11
In article <dj**********@news.ox.ac.uk> rayw <ra*********@gmail.com> wrote:
Could someone give me the name of a compiler/environment where this code
[below] evaluates left to right (or, at least not right to left!) ...
all the gcc implentations I have to hand does this right to left.
So, I'd like to give a concrete counter example.

#include <stdio.h>

int main(void)
{
int n = 1;

/* They'd like this to result in 1, 10, 100.
*/
printf("%d %d %d", n, n *= 10, n *= 10);

return 0;
}


Indeed, as everyone else has noted, this results in undefined
behavior. The question, however, is whether someone has an example
system on which (a) output is produced, and (b) the output is
"1 10 100". (It would help to add a newline.)

The old Pyramid C compiler would have done that, as it evaluated
the first 12 arguments left-to-right. Arguments beyond the first
twelve were evaluated right-to-left (and before the first twelve
were handled left-to-right). This was because most parameters are
passed in the Parameter Registers, pr0 through pr11 inclusive,
while those that do not fit are passed on a conventional stack.
(In the callee, the registers are named pr0 through pr11 -- the
caller addresses them as tr0 through tr11, the temporary or transfer
registers. Normally these are available for scratch computations.
Return values go in pr0 so that the caller sees them in tr0.)

It is generally difficult to find compilers that work strictly
L-to-R: good compilers often seem to do the evaluation in "apparently
random order", while poor compilers usually do them in whatever
order is easiest on the target hardware, and common target hardware
happens to cause the latter to be R-to-L.

Presumably the reason for the desire for "1 10 100" output is to
demonstrate that not all compilers produce "100 10 1", in an effort
to convince programmers that they should avoid such code. But
those who will not be convinced by the text of the C Standards
might also not be convinced by counterexamples: "Why, those compilers
are just broken," they might say.

Still, one handy counterexample I have is a Mac G4 (PowerPC) running
OS X 10.2 (one of these days I should upgrade it). After fixing
the code above to include a terminating newline, and compiling with
"cc" (which is Apple GCC version 1175 "based on gcc version 3.1
20020420 (prerelease)"), I get:

% cc -o t t.c
% ./t
100 10 100

The output remains unchanged when optimized (with -O or -O2).

The PowerPC, MIPS, and SPARC are three good architectures to
try, as all three generally use registers for parameter-passing.

Note that calling a function other than printf() might change the
result on some machines, because printf() is a variable-argument
function, and there are good reasons to want to use different
calling conventions for fixed vs variable argument functions.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 15 '05 #12
Razzer wrote:
Flash Gordon wrote:
rayw wrote:
#include <stdio.h>

int main(void)
{
int n = 1;

/* They'd like this to result in 1, 10, 100.
*/
printf("%d %d %d", n, n *= 10, n *= 10);


This invokes undefined behaviour, which means that literally anything
can happen.
1) n is modified twice without an intervening sequence point which is
undefined behaviour


Isn't the comma operator a sequence point? Moreover, isn't the passing
each argument a sequence point in of itself?


Other have noted there is no comma operator and there no sequence point
between evaluation of parameters.
2) n is used for a purpose other than calculating the new value of n
which I believe also invokes undefined behaviour.


Only if it is in the same expression.


No, between sequence points. In section 6.5 of N1224 it says
| 2 Between the previous and next sequence point an object shall have
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| its stored value modified at most once by the evaluation of an
| expression. Furthermore, the prior value shall be read only to
| determine the value to be stored.71)

No where does it say this only applies to individual expressions. If it
did then it would make this explicit. So
printf("%d %d", n, n *= 10);
would also be undefined behaviour in my opinion since you are reading n
for a purpose other than calculating its new value between sequence points.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #13

"Chris Torek" <no****@torek.net> wrote in message
news:dj*********@news2.newsguy.com...
In article <dj**********@news.ox.ac.uk> rayw <ra*********@gmail.com>
wrote:
Could someone give me the name of a compiler/environment where this code
[below] evaluates left to right (or, at least not right to left!) ...
all the gcc implentations I have to hand does this right to left.
So, I'd like to give a concrete counter example.

#include <stdio.h>

int main(void)
{
int n = 1;

/* They'd like this to result in 1, 10, 100.
*/
printf("%d %d %d", n, n *= 10, n *= 10);

return 0;
}


Indeed, as everyone else has noted, this results in undefined
behavior. The question, however, is whether someone has an example
system on which (a) output is produced, and (b) the output is
"1 10 100". (It would help to add a newline.)

The old Pyramid C compiler would have done that, as it evaluated
the first 12 arguments left-to-right. Arguments beyond the first
twelve were evaluated right-to-left (and before the first twelve
were handled left-to-right). This was because most parameters are
passed in the Parameter Registers, pr0 through pr11 inclusive,
while those that do not fit are passed on a conventional stack.
(In the callee, the registers are named pr0 through pr11 -- the
caller addresses them as tr0 through tr11, the temporary or transfer
registers. Normally these are available for scratch computations.
Return values go in pr0 so that the caller sees them in tr0.)

It is generally difficult to find compilers that work strictly
L-to-R: good compilers often seem to do the evaluation in "apparently
random order", while poor compilers usually do them in whatever
order is easiest on the target hardware, and common target hardware
happens to cause the latter to be R-to-L.

Presumably the reason for the desire for "1 10 100" output is to
demonstrate that not all compilers produce "100 10 1", in an effort
to convince programmers that they should avoid such code. But
those who will not be convinced by the text of the C Standards
might also not be convinced by counterexamples: "Why, those compilers
are just broken," they might say.

Still, one handy counterexample I have is a Mac G4 (PowerPC) running
OS X 10.2 (one of these days I should upgrade it). After fixing
the code above to include a terminating newline, and compiling with
"cc" (which is Apple GCC version 1175 "based on gcc version 3.1
20020420 (prerelease)"), I get:

% cc -o t t.c
% ./t
100 10 100

The output remains unchanged when optimized (with -O or -O2).

The PowerPC, MIPS, and SPARC are three good architectures to
try, as all three generally use registers for parameter-passing.

Note that calling a function other than printf() might change the
result on some machines, because printf() is a variable-argument
function, and there are good reasons to want to use different
calling conventions for fixed vs variable argument functions.


That's just the job - ta.

I should have made it clear that I'm teaching this course, and that the code
was entered by a student. Obviously, I explained the mistake, and then
actually walked through what was happening here with the class.

All I was after -just for interest'ss sake- was the name of a compiler that
happened to eval left to right.

rayw

Nov 15 '05 #14

rayw wrote:
Could someone give me the name of a compiler/environment where this code
[below] evaluates left to right (or, at least not right to left!) - as in
the compiler evaluates the parameter
list left to right. This is for a c programming class, and all the gcc
implentations I have to hand does this right to left. So, I'd like to give
a concrete counter example.

#include <stdio.h>

int main(void)
{
int n = 1;

/* They'd like this to result in 1, 10, 100.
*/
printf("%d %d %d", n, n *= 10, n *= 10);

return 0;
}


The z/OS C version 1 release 6 compiler from IBM for their Mainframes:
(This was compiled and run in batch under JES2)

5694A01 V1 R6 z/OS C 'SYS05297.T091012.RA000.T9232EHD.SOURCE.H02'
* * * * * S O U R C E * * * * *
LINE STMT
*...+....1....+....2....+....3....+....4....+....5 ....+....6....+.
1 |#include <stdio.h>

2 |

3 |int main(void)

4 |{

5 1 | int n = 1;

6 |

7 | /* They'd like this to result in 1, 10, 100.

8 | */

9 2 | printf("%d %d %d", n, n *= 10, n *= 10);

10 |

11 3 | return 0;

12 |}

* * * * * E N D O F S O U R C E * *
Display Filter View Print Options Help
------------------------------------------------------------------
SDSF OUTPUT DISPLAY T9232EHB JOB17312 DSID 102 LINE 0 COL
COMMAND INPUT ===> SCRO
********************************* TOP OF DATA *********************
1 10 100
******************************** BOTTOM OF DATA *******************

Nov 15 '05 #15

"Jalapeno" <ja*******@mac.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...

rayw wrote:
Could someone give me the name of a compiler/environment where this code
[below] evaluates left to right (or, at least not right to left!) - as in
the compiler evaluates the parameter
list left to right. This is for a c programming class, and all the gcc
implentations I have to hand does this right to left. So, I'd like to
give
a concrete counter example.

#include <stdio.h>

int main(void)
{
int n = 1;

/* They'd like this to result in 1, 10, 100.
*/
printf("%d %d %d", n, n *= 10, n *= 10);

return 0;
}


The z/OS C version 1 release 6 compiler from IBM for their Mainframes:
(This was compiled and run in batch under JES2)

5694A01 V1 R6 z/OS C 'SYS05297.T091012.RA000.T9232EHD.SOURCE.H02'
* * * * * S O U R C E * * * * *
LINE STMT
*...+....1....+....2....+....3....+....4....+....5 ....+....6....+.
1 |#include <stdio.h>

2 |

3 |int main(void)

4 |{

5 1 | int n = 1;

6 |

7 | /* They'd like this to result in 1, 10, 100.

8 | */

9 2 | printf("%d %d %d", n, n *= 10, n *= 10);

10 |

11 3 | return 0;

12 |}

* * * * * E N D O F S O U R C E * *
Display Filter View Print Options Help
------------------------------------------------------------------
SDSF OUTPUT DISPLAY T9232EHB JOB17312 DSID 102 LINE 0 COL
COMMAND INPUT ===> SCRO
********************************* TOP OF DATA *********************
1 10 100
******************************** BOTTOM OF DATA *******************


Excellent - thanks!
Nov 15 '05 #16

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

Similar topics

7
by: Reply Via Newsgroup | last post by:
This might sound sad... someone requesting a disertation on the 'eval' statement... but... I've been reading someone else's post - they had a huge calander like script and a handful of folk cursed...
11
by: sneill | last post by:
I have read a number of posts on the use of eval() in Javascript, and I agree that its use is questionable. But it does beg the following question: "How arbitrary does a string need to be before...
0
by: Michelle Keys | last post by:
Subject: DataBinder.Eval Error! Server Error in '/MSPOS' Application. ------------------------------------------------------------------------ -------- DataBinder.Eval:...
18
by: Joe Fallon | last post by:
I have some complex logic which is fairly simply to build up into a string. I needed a way to Eval this string and return a Boolean result. This code works fine to achieve that goal. My...
15
by: manstey | last post by:
Hi, I have a text file called a.txt: # comments I read it using this:
3
by: Pauljh | last post by:
Hi All, I'm running some javascript over a server side generated web page and have multiple generated empty select statements, that I want to populate when the page is loaded. As HTML doesn't do...
4
by: Jm lists | last post by:
Hello members, I want to know does the "eval" in python have the same features as in Perl (capture errors)? For example,in perl I can wrote: $re = eval { 1 / 0 }; Though 1/0 is a fatal...
6
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
5
by: wendallsan | last post by:
Hi all, I'm running into a situation where it seems that JS stops executing as soon as I call an eval in my script. I have an Ajax.Request call to a PHP page that builds a JS object and returns...
10
by: Gordon | last post by:
I have a script that creates new objects based on the value of a form field. Basically, the code looks like this. eval ('new ' + objType.value + '(val1, val2, val3'); objType is a select with...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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.