473,805 Members | 2,007 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1404
rayw <ra*********@gm ail.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)cybers pace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #2

"rayw" <ra*********@gm ail.com> wrote in message
news:dj******** **@news.ox.ac.u k...
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*********@gm ail.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
implentation s 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 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 #9
>> > #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 #10

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

Similar topics

7
4112
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 the script and special attention was thrown at the fact the script used eval alot. I don't use eval alot in my scripts - but I do use it - and since I always out to learn more / improve my javascript skills, I'm curious why something I thought...
11
1889
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 the use of eval() is required to execute it?" Given the following code, I'm able to evaluate/execute most expressions like: "a.b.c.d()"
0
3818
by: Michelle Keys | last post by:
Subject: DataBinder.Eval Error! Server Error in '/MSPOS' Application. ------------------------------------------------------------------------ -------- DataBinder.Eval: 'System.Data.Common.DbDataRecord' does not contain a property with the name REPORTTO. Description: An unhandled exception occurred during the execution of the
18
3165
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 question is what happens to the dynamically created assembly when the method is done running? Does GC take care of it? Or is it stuck in RAM until the ASP.Net process is recycled? This code executes pretty frequently (maybe 4 times per transaction) and...
15
3673
by: manstey | last post by:
Hi, I have a text file called a.txt: # comments I read it using this:
3
1924
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 arrays each select is individually named withe MySelecti where i is an incremental from 1. I know all my variables are correct (i, OptionsCount) and my arrays of MyValues and MyDescription's exist for multiple enteries and if I bring out an...
4
2795
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 error but since it's in "eval" block so the perl
6
5933
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 or without parms. I know that any function that is passed to Eval() must be declared Public. It can be a Sub or Function, as long as it's Public. I even have it where the "function" evaluated by Eval can be in a form (class) module or in a standard...
5
6638
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 it as the responseText. I want to attach that object to the document (or anywhere else that would allow me to access it later), and to do that, I THINK I need to eval it because it's just a string otherwise. My problem is as soon as I execute a...
10
494
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 the different types of objects you can create as values. I really don't like using eval, and it's causing problems, like if I do something like the following:
0
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10604
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10356
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
10361
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
9179
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...
0
6874
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
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.