i ++ * i ++ [英] i++ * i++
问题描述
#include< stdio.h>
#define PRODUCT(x)(x * x)
int main()
{
int i = 3,j,k;
j = PRODUCT(i ++);
k = PRODUCT (++ i);
printf(" \ n%d%d",j,k);
return( 0);
}
-------------------------- ---------------------------------
输出为9和49 >
答案不应该是12和20?
编译器达到上述给定输出的步骤是什么?
rohit写道:#include< stdio.h>
#define PRODUCT(x)(x * x)
这是一个问题:考虑PRODUCT(2 + 2),扩展到
2 + 2 * 2 + 2,
即8而不是16,但这不是造成这个特殊问题的原因。
int main()
int i = 3,j,k ;
j = PRODUCT(i ++);
这是未定义的行为:它扩展为i ++ * i ++
k = PRODUCT(++ i);
printf(" ; \ n%d%d",j,k);
返回(0);
}
--------- --------------------------------------------------
输出为9和49
好的。
答案不应该是12和20吗?
不是。它可以是任何东西。是的,什么。
编译器达到上述给定输出的步骤是什么?
无论感觉如何。
阅读常见问题解答(从问题3.2开始,这正是这个
问题)。 (c-faq.com)
您可能还会发现
http://www.eskimo.com/~scs/readings/undef.981105.html 有用。
有实际上是一个非常明智的解释,该程序如何计算9和49,但是尝试二次猜测未定义
行为是一个坏主意。
-thomas
rohit< ro ******* @ gmail.com>写道:#include< stdio.h>
#define PRODUCT(x)(x * x)
int main()
{
int i = 3,j,k;
j = PRODUCT(i ++);
k = PRODUCT(++ i);
printf (\ n%d%d,j,k);
返回(0);
}
------- -------------------------------------------------- -
输出为9和49
无论如何,对于硬件平台上的编译器。
应该不是答案12和20?
答案是序列点之间的多个赋值会产生
未定义的行为。
-
www.designacourse.com
培训任何人的最简单方法......无处不在。
Chris Smith - 首席软件开发人员/技术培训师
MindIQ Corporation
rohit写道:#include< stdio.h>
#define PRODUCT(x)(x * x)
int main()
{
int i = 3,j,k;
j = PRODUCT(i ++);
k = PRODUCT(++ i);
printf(" \ n%d %d,j,k);
返回(0);
}
输出为9和49
答案是12和20?
#include <stdio.h>
#define PRODUCT(x) (x*x)
int main()
{
int i =3,j,k;
j = PRODUCT(i++);
k = PRODUCT(++i);
printf("\n%d %d", j,k);
return (0);
}
-----------------------------------------------------------
The output is 9 and 49
shouldn''t the answer be 12 and 20?
What are the steps the compiler takes to reach the above given output?
rohit wrote:#include <stdio.h>
#define PRODUCT(x) (x*x)
This is asking for trouble: consider PRODUCT(2+2), which expands to
2+2*2+2,
ie 8 rather than 16, but that isn''t what is causing this particular
problem.
int main()
{
int i =3,j,k;
j = PRODUCT(i++);
This is undefined behaviour: it expands to i++*i++
k = PRODUCT(++i);
printf("\n%d %d", j,k);
return (0);
}
-----------------------------------------------------------
The output is 9 and 49
Ok.
shouldn''t the answer be 12 and 20?
No. It can be anything. Yes, anything.
What are the steps the compiler takes to reach the above given output?
Whatever it feels like.
Read the FAQ (starting with question 3.2, which is exactly this
question). (c-faq.com)
You might also find
http://www.eskimo.com/~scs/readings/undef.981105.html useful.
There is actually a perfectly sensible explanation for how the program
might have computed 9 and 49, but trying to second-guess undefined
behaviour is a bad idea.
-thomas
rohit <ro*******@gmail.com> wrote:#include <stdio.h>
#define PRODUCT(x) (x*x)
int main()
{
int i =3,j,k;
j = PRODUCT(i++);
k = PRODUCT(++i);
printf("\n%d %d", j,k);
return (0);
}
-----------------------------------------------------------
The output is 9 and 49
For your compiler on your hardware platform, anyway.
shouldn''t the answer be 12 and 20?
The answer is that multiple assignments between sequence points yield
undefined behavior.
--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
rohit wrote:#include <stdio.h>
#define PRODUCT(x) (x*x)
int main()
{
int i =3,j,k;
j = PRODUCT(i++);
k = PRODUCT(++i);
printf("\n%d %d", j,k);
return (0);
}
The output is 9 and 49
shouldn''t the answer be 12 and 20?
You''ll want to read http://c-faq.com/expr/
这篇关于i ++ * i ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!