0

Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
function parameter evaluation order

enter code hereIn C language execution of function is from right to left or left to right? I found it is from right to left.(Execution of function means in which order it pass the argument).

So i want to know is there any case of function or any inbuilt function in c which execute from left to right?

EXAMPLE:-

#include<stdio.h>
int print(int a,int b);
int main()
{
    int a=10,b=20;
    print(++a,a++);
    getch();
    return 0;
}

int print(int a,int b)
{
    printf("%d %d",a,b);
}

So in this it starts from right side and pass a=10 than a++ so now a=11 than ++a so now a=12 than it pass a=12 so in print function it prints 12 10

Community
  • 1
  • 1
Raj
  • 252
  • 1
  • 5
  • 20
  • 1
    See also: [Defined argument evaluation order leads to sub-optimal code](http://stackoverflow.com/questions/11450093/defined-argument-evaluation-order-leads-to-sub-optimal-code) for an explanation of the historical basis for the order being unspecified: – verdesmarald Oct 02 '12 at 06:19
  • 1
    `printf(++a,a++)` yields undefined behavior. You cannot rely on it. – Alexey Frunze Oct 02 '12 at 06:26

1 Answers1

7

The order of evaluation of function arguments is Unspecified.
The c standard gives implementations full freedom to evaluate them in:

  • Left to Right or
  • Right to Left or
  • Any other magical order

Also, the implementation is not needed to specify which ordering it follows.


References:

C99 Standard 6.5.2.2 Function calls
Para 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.

C99 Standard 3.19:

unspecified behavior
behavior where this International Standard provides two or more possibilities and imposes no requirements on which is chosen in any instance.

EXAMPLE An example of unspecified behavior is the order in which the arguments to a function are evaluated.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • They made this definition a bit more obscure in C11, but the meaning is essentially the same: `"There is a sequence point after the evaluations of the function designator and the actual arguments but before the actual call. Every evaluation in the calling function (including other function calls) that is not otherwise specifically sequenced before or after the execution of the body of the called function is indeterminately sequenced with respect to the execution of the called function."` – Lundin Oct 02 '12 at 06:29
  • 1
    Also, "unspecified" means the behavior need not be consistent between different executions of the same code. You'd think it probably will be, just because of how compilers work, but "any other magical order" permits "selecting an order at random each time through the code". Or more plausibly "the optimizer selecting an order based on what registers it has free and what partial results it needs elsewhere, which therefore can change because of apparently-unrelated changes elsewhere in the function that contains the function call". – Steve Jessop Oct 02 '12 at 09:08