转让声明的评估顺序 [英] Evaluation order of assignment statement

查看:86
本文介绍了转让声明的评估顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C规范是否定义了分配评估的顺序

语句?


例如,以下输出应该是什么:


int foo1(){printf(" foo1 \ n");返回0; }

int foo2(){printf(" foo2 \ n");返回0; }

int foo3(){printf(" foo3 \ n");返回0; }


int main()

{

int array [1];

array [ foo1()] = foo2()+ foo3();

}


我问这个问题,因为我得到了相互矛盾的结果

不同的编译器,想要了解这是否是编译器

bug。


gcc print:foo1,foo2 ,foo3。

Microsoft Visual C打印:foo2,foo3,foo1。


谢谢。

解决方案

na****@gmail.com 写道:


C规范是否定义了赋值的评估顺序

语句?


例如,应该是什么输出如下:


int foo1(){printf(" foo1 \ n");返回0; }

int foo2(){printf(" foo2 \ n");返回0; }

int foo3(){printf(" foo3 \ n");返回0; }


int main()

{

int array [1];

array [ foo1()] = foo2()+ foo3();



这是因为函数foo2(),

foo3()或foo1()的执行顺序是未定义的。

编译器没什么问题。


无法分辨这三者的哪个功能将会是什么?
首先打电话。


}


我问这个问题,因为我收到了相反的结果

不同的编译器,并想了解这是否是一个编译器

bug。


gcc print:foo1,foo2, foo3。

Microsoft Visual C打印:foo2,foo3,foo1。



John

-

John Smith


< blockquote>文章< 11 ********************** @ e3g2000cwe.googlegroups。 com>,

< na **** @ gmail.comwrote:


> C规范是否定义了评估顺序作业
声明?


>例如,以下输出应该是什么:


> int foo1(){printf(" foo1 \ n");返回0; }
int foo2(){printf(" foo2 \ n");返回0; }
int foo3(){printf(" foo3 \ n");返回0; }


> int main()

int array [1];

array [foo1()] = foo2()+ foo3();
}



这不是一个关于

赋值语句的评价顺序:这是关于

评估表达式组件的顺序的问题。


在大多数情况下,答案是否定的。


完整的答案涉及关于序列点的复杂规则,并且

有关于规则在不明确的情况下意味着什么的持续争论

情况。即使是经验丰富的职业选手也不会总是同意(至少对于前十几个火焰来说是b $ b)关于序列点的影响

在一些不寻常的表达中。

-

重要的是要记住,在法律方面,电脑

永远不会复制,只有人类才能制作复制品。计算机给出了

命令,而非许可。只有人才可以获得许可。

- Brad Templeton


na **** @ gmail.com 写道:


C规范是否定义了作业评估的顺序

报表?



否。其余的如下。


-

祝你好运,
Andrey Tarasevich


Does the C specification define the order of evaluation of assignment
statements?

For example, what should be the output from the following:

int foo1() { printf("foo1\n"); return 0; }
int foo2() { printf("foo2\n"); return 0; }
int foo3() { printf("foo3\n"); return 0; }

int main()
{
int array[1];
array[foo1()] = foo2() + foo3();
}

I''m asking this question since I''m getting conflicting results on
different compilers, and want to understand whether this is a compiler
bug or not.

gcc prints: foo1, foo2, foo3.
Microsoft Visual C prints: foo2, foo3, foo1.

Thanks.

解决方案

na****@gmail.com wrote:

Does the C specification define the order of evaluation of assignment
statements?

For example, what should be the output from the following:

int foo1() { printf("foo1\n"); return 0; }
int foo2() { printf("foo2\n"); return 0; }
int foo3() { printf("foo3\n"); return 0; }

int main()
{
int array[1];
array[foo1()] = foo2() + foo3();

It''s because the order of execution of the functions foo2(),
foo3() or foo1() is undefined. There''s nothing wrong with
the compilers.

There no way to tell which function of the three is gonna
get called first.

}

I''m asking this question since I''m getting conflicting results on
different compilers, and want to understand whether this is a compiler
bug or not.

gcc prints: foo1, foo2, foo3.
Microsoft Visual C prints: foo2, foo3, foo1.

John
--
John Smith


In article <11**********************@e3g2000cwe.googlegroups. com>,
<na****@gmail.comwrote:

>Does the C specification define the order of evaluation of assignment
statements?

>For example, what should be the output from the following:

>int foo1() { printf("foo1\n"); return 0; }
int foo2() { printf("foo2\n"); return 0; }
int foo3() { printf("foo3\n"); return 0; }

>int main()
{
int array[1];
array[foo1()] = foo2() + foo3();
}

That isn''t really a question about the order of evaluation of
assignment statements: it''s really a question about the order of
evaluation of the components of an expression.

For the most part, the answer is NO.

The full answer involves complex rules about "sequence points", and
there are continual arguments about what the rules mean in obscure
circumstances. Even seasoned pros don''t always agree (at least
for the first dozen flames) about the implications of sequence points
in some unusual expressions.
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton


na****@gmail.com wrote:

Does the C specification define the order of evaluation of assignment
statements?

No. The rest follows.

--
Best regards,
Andrey Tarasevich


这篇关于转让声明的评估顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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