这是编译器的错误吗? [英] Is this a bug of the compiler?
问题描述
以下代码:
----------------------------------- -
#include< stdio.h>
int a = 10
main()
{ printf(a =%d a ++ =%d ++ a =%d \ n,a,a ++,++ a);
返回0;
}
------------------------------------
使用gcc时,输出为:
a = 12 a ++ = 10 ++ a = 12
使用VC ++时,输出为:
a = 11 a ++ = 11 ++ a = 11
它们似乎都不合理。
任何人都可以给出指针这个?
" John" <乔******* @ yahoo.com>在消息中写道
news:9a ************************** @ posting.google.c om ...以下代码:
---------------------------------- ---
#include< stdio.h>
int a = 10
main()
{printf(" a =%d a ++ =%d ++ a =%d \ n",a,a ++,++ a);
返回0;
}
分隔参数的逗号不构成一个序列
点。
允许编译器以它认为合适的任何
顺序解析a ++和++一个子表达式。 />
(我希望你会想要:10,10,11,但你很少得到:-) ----------------- -------------------
使用gcc时,输出为:
a = 12 a ++ = 10 ++ a = 12
imo这一定是个bug。只有++ a才能实际增加a。你应该
永远看不到12.
使用VC ++时,输出是:
a = 11 a ++ = 11 ++ a = 11
这是正常的。
您的代码示例必须具有未定义的行为。
问候,
Conrad Weyns。
它们似乎都不合理。
任何人都可以指出这个吗?
John写道:以下代码:
-------------------------------------
#include< stdio.h>
int a = 10
main()
{printf(" a =%d a ++ =%d ++ a =%d \\ \\ n,a,a ++,++ a);
返回0;
}
--------------------- ---------------
使用gcc时,输出为:
a = 12 a ++ = 10 ++ a = 12
当使用VC ++时,输出为:
a = 11 a ++ = 11 ++ a = 11
它们似乎都不合理。
任何人都可以给出指针吗?
哇....在我看来这是一个很好的问题。
>
我用cout验证它:
#include< iostream>
使用命名空间std;
int main()
{
int a = 10;
cout<< a<<英寸;" << a ++<<英寸;" << ++ a<<结束;
返回0;
}
得到输出:
12; 11 ; 11
似乎流向后处理属性。
这被认为是这样吗?
关于marbac
John在新闻中写道:9a ************************** @ posts.google.c om
comp.lang.c ++:
以下代码:
--------- ----------------------------
#include< stdio.h>
int a = 10
main()
{printf(" a =%d a ++ =%d ++ a =%d \ n",a,a ++,++ a);
返回0;
}
------------------------------------
使用时gcc,输出为:
a = 12 a ++ = 10 ++ a = 12
使用VC ++时,输出为:
a = 11 a ++ = 11 ++ a = 11
它们似乎都不合理。
任何人都能指出这个吗?
它们都是合理的,在C ++你可能不会修改av的
值序列点之间不止一次。
在上面的''''在序列点之前被修改两次
在'';'' ,该程序显示未定义的行为,任何可能发生的事情。
BTW main()返回int并且你错过了'';''之后
对''''的定义。
Rob。
-
http://www.victim-prime.dsl.pipex.com/
The following code:
-------------------------------------
#include <stdio.h>
int a=10
main()
{ printf("a=%d a++=%d ++a=%d \n",a, a++,++a);
return 0;
}
------------------------------------
When using gcc, the output is:
a=12 a++=10 ++a=12
When using VC++, the output is:
a=11 a++=11 ++a=11
None of them seems reasonable.
Can anybody give a pointer on this?
"John" <jo*******@yahoo.com> wrote in message
news:9a**************************@posting.google.c om...The following code:
-------------------------------------
#include <stdio.h>
int a=10
main()
{ printf("a=%d a++=%d ++a=%d \n",a, a++,++a);
return 0;
}
The comma that separates the parameters does not constitute a sequence
point.
The compiler is allowed to resolve the a++ and ++a subexpresions in any
order it sees fit.
(I expect you''d want: 10, 10, 11 but you very rarely get that :-) ------------------------------------
When using gcc, the output is:
a=12 a++=10 ++a=12
i.m.o this must be a bug. Only ++a can actually increment a. You should
never see 12.
When using VC++, the output is:
a=11 a++=11 ++a=11
This however is normal.
Your code example has essentialy "undefined behaviour".
Regards,
Conrad Weyns.
None of them seems reasonable.
Can anybody give a pointer on this?
John wrote:The following code:
-------------------------------------
#include <stdio.h>
int a=10
main()
{ printf("a=%d a++=%d ++a=%d \n",a, a++,++a);
return 0;
}
------------------------------------
When using gcc, the output is:
a=12 a++=10 ++a=12
When using VC++, the output is:
a=11 a++=11 ++a=11
None of them seems reasonable.
Can anybody give a pointer on this?
Wow .... thats a good question in my view.
I used cout to verify it:
#include <iostream>
using namespace std;
int main()
{
int a=10;
cout << a << ";" << a++ << ";" << ++a << endl;
return 0;
}
and got the output:
12;11;11
Seems that the stream processes the attributes backwards.
Is this thought to be so?
regards marbac
John wrote in news:9a**************************@posting.google.c om in
comp.lang.c++:
The following code:
-------------------------------------
#include <stdio.h>
int a=10
main()
{ printf("a=%d a++=%d ++a=%d \n",a, a++,++a);
return 0;
}
------------------------------------
When using gcc, the output is:
a=12 a++=10 ++a=12
When using VC++, the output is:
a=11 a++=11 ++a=11
None of them seems reasonable.
Can anybody give a pointer on this?
They are all reasonable, in C++ you may not modify the
value of a variable more than once between sequence points.
In the above ''a'' is modified twice before the sequence point
at the '';'', the programme exhibts Undefined Behaviour, anything
can happen.
BTW main() returns int and you''re missing a '';'' after the
defenition of ''a''.
Rob.
--
http://www.victim-prime.dsl.pipex.com/
这篇关于这是编译器的错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!