Arg Eval [英] Arg Eval

查看:80
本文介绍了Arg Eval的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以给我一个编译器/环境的名称,其中这段代码

[下面]从左到右评估(或者,至少从右到左评估!) - 如

编译器从左到右评估参数

列表。这是一个c编程类,我必须提供的所有gcc

实现从右到左。所以,我想给

a具体的例子。


#include< stdio.h>


int main(无效)

{

int n = 1;


/ *他们想要这个结果是1,10,100。

* /

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


返回0;

}

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;
}

推荐答案

rayw< ra ********* @ gmail.com>写道:
rayw <ra*********@gmail.com> wrote:
#include< stdio.h>
int main(无效)
{
int n = 1;
/ *他们希望这会产生1,10,100。
* /


如果他们是你的导师,立即放弃课程并告诉他们学习


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


之前他们假定在C上教课。

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


这是错误的代码,即使编译器/实现按照它的顺序告诉你,你也绝不能期望它产生可靠的结果
结果/>
计算函数参数(它可能没有,因为标准

允许它这样做。)

返回0;
}
#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 |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



--
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.




" rayw" < RA ********* @ gmail.com>在留言中写道

news:dj ********** @ news.ox.ac.uk ...

"rayw" <ra*********@gmail.com> wrote in message
news:dj**********@news.ox.ac.uk...
有人能给我一个名字吗?一个编译器/环境,这个代码
[下面]从左到右评估(或者,至少从右到左评估!) - 如同编译器从左到右评估参数
列表。这是针对交流编程课程,


如果你的导师告诉你要编写代码

,那么你需要一位新教师。

以及我必须提供的所有gcc
实现从右到左。所以,我想
给出一个具体的反例。


语言标准没有指定

评估函数参数的顺序;因此,一个

不应该编写依赖它的代码。

#include< stdio.h>

int main(void)
{
int n = 1;

/ *他们希望这导致1,10,100。
* /
printf(" ;%d%d%d,n,n * = 10,n * = 10);

返回0;
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);





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


这将始终产生:

1 10 100


....无论编译器如何。


-Mike



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

rayw写道:
有人可以给我一个编译器/环境的名称,这个代码
[下面]从左到右评估(或者,至少从右到左评估! ) - 如在
中编译器从左到右评估参数
列表。


不在这里,或者至少不可靠,因为

参数的评估顺序未按照标准指定,所以编译器

可以生成代码,在每次运行时以不同的顺序对它们进行评估。

这是针对交流编程类的,我必须提供的所有gcc
实现这样做是正确的剩下。所以,我想给出一个具体的反例。


如果你被要求在课堂上这样做,那么这是一个毫无意义的

作业。

#include< stdio .h>

int main(无效)
{
int n = 1;

/ *他们希望这导致1 printf("%d%d%d,n,n * = 10,n * = 10); 10,100。


这会调用未定义的行为,这意味着确实可以发生任何事情



1)n被修改两次而没有干预序列点是

未定义的行为

2)n用于计算新值n

之外的其他用途我相信调用未定义的行为。


我当然可以通过实际方式产生10 10 10作为输出。


这与所有内容密切相关comp.lang.c常见问题解答,大多数是明确的 http://www.eskimo.com/~scs/C-faq/q3.2.html

返回0;
}
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

生活在有趣的时代。

虽然我的电子邮件地址说垃圾邮件,它是真实的,我读了它。


--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.


这篇关于Arg Eval的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆