tags:

views:

131

answers:

2

Possible Duplicate:
Why is i = ++i + 1 unspecified behavior?

Consider the following snippet :

int i=10;
printf("%d %d %d",i,++i,i--);

The order in which the arguments to a function are evaluated is unspecified in C/C++.So it lead to unspecified behavior.

Am I correct or missing something ? Please Explain.

EDIT:Well,some member believes it to be duplicate and this is an Undefined behaviour.Anyways,from C99:

6.5.2.2(10)

The order of evaluation of the function designator, the actual arguments, and subexpressions within the actual arguments is unspecified, but there is a sequence point before the actual call.

So what would be the exact nomenclature now,Undefined or Unspecified ?

+2  A: 

Yes, true.

I take it it's because on different platforms different machinery is employed to pass arguments and therefore parameters may be evaluated in different order.

Michael Krelin - hacker
+1  A: 

What you're seeing is an example of where the C/C++ spec is undefined, so different compilers can do whatever they want. One compiler might execute the parameters in left to right order, another might do it in right to left order. It would be perfectly OK for a compiler to pick the order randomly.

The point that your source is trying to make is that you shouldn't rely on any order when passing parameters. For example if you had:

A(DoX(), DoY())

DoX and DoY can't rely on any side-effects of the other, because they're executed in an undefined order. To be perfectly explicit you'd want to do something like:

int x = DoX();
int y = DoY();
A(x, y);

For the majority of real-world production code you don't run into this situation very often, but it does happen every now and again.

Note that this is related to, but different from short circuit evaluation.

popester