开发者

different outputs on different compiler [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

FAQ : Undefined Behavior and Sequence Points

Different outputs on different compiler? Is this true that certain statements can generate different outputs on different compilers. I have two compilers handy gcc and msvc ex开发者_StackOverflowpression edition. When I tried a code sample on both of them I was shocked to see different outputs on them.

This was the code sample.

#include<stdio.h>
int main(void)
{
int variable_a = 100;
printf("%d %d", ++variable_a, variable_a++); return 0;
}

Output that I got on gcc was 102 100

On msvc I got 102 101.

Why such a difference?


You invoke undefined behaviour by incrementing a more than once. Any compiler would be within their rights to break into your house and beat you with a stick.


There are various subtle effects of this kind where the language is explicitly undefined. There's a lot of history behind why the language leaves these corners undefined. From the coder's point of view we need to avoid certain patterns such as the one you stumbled across.

See this reference for some explanation

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜