1

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 ?

Community
  • 1
  • 1
  • 3
    Duplicate: http://stackoverflow.com/questions/1860461/why-is-i-i-1-unspecified-behavior – GManNickG Dec 18 '09 at 21:18
  • 1
    And the term is undefined: It leads to "undefined" behavior. You could turn into a cat. – GManNickG Dec 18 '09 at 21:19
  • Duplicates: http://stackoverflow.com/questions/621542/compilers-and-argument-order-of-evaluation-in-c http://stackoverflow.com/questions/376278/parameter-evaluation-order-before-a-function-calling-in-c – Greg Hewgill Dec 18 '09 at 21:20
  • How that is duplicate of this ? :O –  Dec 18 '09 at 21:21
  • @GMan : Please explain, how this leads to undefined behaviour ? –  Dec 18 '09 at 21:22
  • Go to the duplicate question. The reason I'm linking you there is so we don't have to repeat information. :P – GManNickG Dec 18 '09 at 21:24
  • That said, Greg's duplicates are probably more relevant. – GManNickG Dec 18 '09 at 21:25
  • First, never do this. Second, maybe a typo, but you want to print three numbers and you only have two %d. It should be "%d%d%d". – Jabba Dec 18 '09 at 21:25
  • It leads to undefined behaviour because the C and C++ Standards say it does. –  Dec 18 '09 at 21:25
  • @Jabba:Thanks,for pointing,it's a typo. –  Dec 18 '09 at 21:28
  • @Jabba, I also thought about it, but then there are more parameters than placeholders, not vice versa ;-) – Michael Krelin - hacker Dec 18 '09 at 21:30
  • @ Neil Butterworth : Do you mean order of evaluation of function argument is UB by C standard ? Can you please explain `6.5.2.2(10)` C99. –  Dec 18 '09 at 21:48
  • @GMan Your duplicate is not related at all, you should read the questions entirely. This is about the order of evaluation of parameters, where the other one is about operators priority. Fixed. (btw you've even answered "Go to the duplicate question" though you have not read the OP one :/) – KeatsPeeks Dec 18 '09 at 21:54
  • @TheSamFrom1984: Exactly,I already mentioned it,but he retorts to sarcasm.I don't want to reinvent the wheel either but that link doesn't have my answer. –  Dec 18 '09 at 21:58
  • 1
    The order of evaluating arguments is unspecified, meaning the compiler has to evaluate all of them, but can arbitrarily order them. However, there are no sequence points here. You're modifying a variable more than once without an intervening sequence point, and that's undefined behavior regardless of the order of evaluation. – David Thornley Dec 18 '09 at 21:58
  • Thanks David I understood it now :) –  Dec 18 '09 at 22:03
  • http://stackoverflow.com/questions/376278/parameter-evaluation-order-before-a-function-calling-in-c This is the more accurate possible duplicate. – whacko__Cracko Dec 19 '09 at 11:25

2 Answers2

2

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
  • 122,635
  • 21
  • 184
  • 169
1

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
  • 1,896
  • 9
  • 12