Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Arg Eval

Reply
Thread Tools

Arg Eval

 
 
rayw
Guest
Posts: n/a
 
      10-21-2005
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;
}



 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      10-21-2005
rayw <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      10-21-2005

"rayw" <(E-Mail Removed)> wrote in message
news:djbpg3$jrb$(E-Mail Removed)...
> 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



 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      10-22-2005
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.
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      10-22-2005
On Fri, 21 Oct 2005 23:18:43 +0100, "rayw" <(E-Mail Removed)>
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>>
 
Reply With Quote
 
Razzer
Guest
Posts: n/a
 
      10-22-2005

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.

 
Reply With Quote
 
Artie Gold
Guest
Posts: n/a
 
      10-22-2005
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!"
 
Reply With Quote
 
Razzer
Guest
Posts: n/a
 
      10-22-2005

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!"


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      10-22-2005

"Razzer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      10-22-2005
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
eval('07') works, eval('08') fails, why? Alex van der Spek Python 6 01-08-2009 08:24 PM
How to pass a multiline arg to exec('some.exe arg')? n00m Python 5 05-05-2008 02:58 PM
DataBinder.Eval and Eval. craigkenisston@hotmail.com ASP .Net 1 06-16-2006 05:33 PM
Trouble with setTimeout(arg, arg) nat.hourt@gmail.com Javascript 7 11-12-2005 05:13 PM
DataBinder.Eval for an object's property property... like Eval(Container.DataItem,"Version.Major") Eric Newton ASP .Net 3 04-04-2005 10:11 PM



Advertisments