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

荒凉一梦 提交于 2019-12-13 19:43:01

问题


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

void 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.


回答1:


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.



来源:https://stackoverflow.com/questions/5115628/c-program-gives-an-output-different-on-different-compiler

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!