表达评价的特殊问题 [英] peculiar problem with expression evaluation

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

问题描述

大家好,


我发现了令人惊讶的事情:

这里是代码片段


*** **************

int i = 5;

i = i ++ +((((8 << 1)< ;<(5 << 1))* 3 * 3))+ ++ i;

printf(" i =%d \ n",i);

*****************

输出

i = 147469

但是,如果我只是将中间表达式(即(((<<< 1)<<(5<<

1))* 3 * 3))分配给一个整数k我的输出变化


**************

int i = 5;

int k = 0;

k =((((8 << 1)<<(5<< 1> * * 3 * 3));

i = i ++ + k + ++ i;

printf(" i =%d \ n",i);

**** ***********

输出

i = 147468


据我所知postfix增量应该仅在案例2中的

表达式结束时进行评估。但是在案例1中,它在评估过程中会增加




我无法弄明白为什么我会得到这个差异

输出。


提前致谢

Sarab

解决方案

sa********@gmail.com 写道:
< blockquote class =post_quotes>
大家好,


我发现了令人惊讶的事情:

这是代码片段

*****************

int i = 5;

i = i ++ +(((( 8 << 1<<<(5<< 1)* 3 * 3))+ ++ i;

printf(" i =%d \ n ,i);

*****************



请参阅以前关于此形式的未定义

行为的分数。


-

Ian Collins。


忘了告诉我的编译器版本。我正在使用gcc编译器即

gcc(GCC)3.2.3 20030502(Red Hat Linux 3.2.3-47)


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


忘了告诉我的编译器版本。我正在使用gcc编译器即

gcc(GCC)3.2.3 20030502(Red Hat Linux 3.2.3-47)



我们不喜欢不关心那个。你用什么版本的C标准

咨询?你忽略了什么版本的C-FAQ?


一些建议: -


*阅读c-faq的常见问题解答。 com $ />

*在Google中搜索此群组以获取序列点


*不要浪费我们的时间和网络带宽


Hi All,

I observed something surprising:
Here is code snippet

*****************
int i = 5;
i = i++ + ((((8 << 1) << (5 << 1)) * 3 * 3)) + ++i;
printf ( "i = %d \n " ,i);
*****************
output
i = 147469
But if i just assign the middle expression i.e. ((((8 << 1) << (5 <<
1)) * 3 * 3)) to an integer k my output changes

**************
int i = 5;
int k = 0;
k = ((((8 << 1) << (5 << 1)) * 3 * 3));
i = i++ + k + ++i;
printf ( "i = %d \n " ,i);
***************
output
i = 147468

As far as i know postfix increment should evaluate only at the end of
expression as in case 2 . but in case 1 it is getting incremented
during evaluation .

I am not able to figure out why i am getting this difference in
output.

Thanks in advance
Sarab

解决方案

sa********@gmail.com wrote:

Hi All,

I observed something surprising:
Here is code snippet

*****************
int i = 5;
i = i++ + ((((8 << 1) << (5 << 1)) * 3 * 3)) + ++i;
printf ( "i = %d \n " ,i);
*****************

Please see the scores of previous postings about this form of undefined
behavior.

--
Ian Collins.


Forgot to tell my compiler version . i am using gcc compiler i.e.
gcc (GCC) 3.2.3 20030502 (Red Hat Linux 3.2.3-47)


sa********@gmail.com wrote:

Forgot to tell my compiler version . i am using gcc compiler i.e.
gcc (GCC) 3.2.3 20030502 (Red Hat Linux 3.2.3-47)

We don''t care about that. What version of the C standard did you
consult? What version of the C-FAQ did you neglect to read?

Some suggestions:-

* Read the FAQ at c-faq.com

* Search this group in Google for "sequence point"

* Don''t waste our time and network bandwidth


这篇关于表达评价的特殊问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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