开发者

C program gives an output different on different compiler [duplicate]

开发者 https://www.devze.com 2023-02-13 22:27 出处:网络
This question already has answers here: Why are these constructs using pre and post-increment undefined behavior?
This question already has answers here: Why are these constructs using pre and post-increment undefined behavior? (14 answers) Closed 8 years ago.

I ran a C program and got different output on different C compilers. Below is my program

void 开发者_JS百科main()
{
    int i=5;
     printf("%d%d%d%d%d",i++,i--,++i,--i,i);
}

ON boarnland c++ complier o/p is

45545

and on gcc its

45555

is it really compiler dependent or its OS dependent?

The arguments in a function call are pushed into the stack from left to right. The evaluation is by popping out from the stack. and the evaluation is from right to left, hence the result.


You cannot rely on the order of execution of side effects to arguments to a function. In this case the 2 compilers are executing the side effects in a different order, producing different results.

0

精彩评论

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