前/后增量蓝调 [英] pre/post increment blues

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

问题描述

代码:


int aaa = 100;

printf("%d%d%d \ n", - aaa, aaa,aaa - );

printf("%d \ n",aaa);


打印:

99 100 100




有人可以解释这背后的逻辑吗?

解决方案

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


代码:


int aaa = 100;

printf("%d%d%d \ n", - aaa,aaa,aaa--);

printf("%d \ n", aaa);


打印:


99 100 100

98


有人可以解释这背后的逻辑吗?



是的。空无一人。它是未定义的行为。


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


代码:


int aaa = 100;

printf("%d%d%d \ n", - aaa,aaa,aaa--);

printf (%d \ n,aaa);


打印:


99 100 100

98

有人可以解释这背后的逻辑吗?



这背后没有逻辑。在语句中多次修改

变量是未定义的行为。阅读编译器

(可能/希望)输出的警告。任何事情都可能发生。


请阅读:
http://www.parashift.com/c++-faq-lit...html#faq-39.15


-

rbh


On Thu,2007年6月14日19:12:28 -0700," di在con.lang.c ++中,************ @ gmail.com"

< di ********** @ gmail.comwrote:


代码:


int aaa = 100;

printf("%d% d%d \ n", - aaa,aaa,aaa--);

printf("%d \ nn",aaa);


打印:


99 100 100

98


有人可以解释这背后的逻辑吗?



这背后没有逻辑。你写了一个声明,它修改了一个对象的值两次,并另外读取了另一个没有参与计算新值的时间。所有没有

之间的任何序列点。


行为未定义,C ++标准未指定或

care会发生什么。


-

杰克克莱恩

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c < a rel =nofollowhref =http://c-faq.com/target =_ blank> http://c-faq.com/

comp。 lang.c ++ http://www.parashift.com/c++-faq- lite /

alt.comp.lang.learn.c-c ++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html


The code:

int aaa = 100;
printf("%d %d %d\n", --aaa, aaa, aaa--);
printf("%d\n", aaa);

prints:

99 100 100
98

could somebody explain the logic behind this?

解决方案

di************@gmail.com wrote:

The code:

int aaa = 100;
printf("%d %d %d\n", --aaa, aaa, aaa--);
printf("%d\n", aaa);

prints:

99 100 100
98

could somebody explain the logic behind this?

Yes. There is none. It''s undefined behaviour.


di************@gmail.com wrote:

The code:

int aaa = 100;
printf("%d %d %d\n", --aaa, aaa, aaa--);
printf("%d\n", aaa);

prints:

99 100 100
98

could somebody explain the logic behind this?

There is no logic behind this. It is undefined behaviour to modify a
variable more than once in a statement. Read the warnings the compiler
(presumably/hopefully) output. Anything could happen.

Please read this:
http://www.parashift.com/c++-faq-lit...html#faq-39.15

--
rbh


On Thu, 14 Jun 2007 19:12:28 -0700, "di************@gmail.com"
<di**********@gmail.comwrote in comp.lang.c++:

The code:

int aaa = 100;
printf("%d %d %d\n", --aaa, aaa, aaa--);
printf("%d\n", aaa);

prints:

99 100 100
98

could somebody explain the logic behind this?

There is no logic behind this. You wrote a statement that modifies
the value of an object twice, and additionally reads the value another
time not involved in the calculation of the new value. All without
any sequence points in between.

The behavior is undefined, and the C++ standard does not specify or
care what happens.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html


这篇关于前/后增量蓝调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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