0

Possible Duplicate:
Is this undefined C behaviour?

#include<stdio.h>
int main()
{
    int a=5;
    printf("%d %d %d",a++,a++,++a);
    return 0;
}

Output:

In gcc:

7 6 8

In TURBO C:

7 6 6
Community
  • 1
  • 1
sarsarahman
  • 1,038
  • 5
  • 11
  • 25

2 Answers2

7

Because the order of evaluation of arguments to a function is unspecified and may vary from compiler to compiler. An compile may evaluate function arguments from:
left to right or
right to left or
in any other pattern.

This order is not specified by the C standard.

Reference:

C99 Standard 6.5

"The grouping of operators and operands is indicated by the syntax.72) Except as specified later (for the function-call (), &&, ||, ?:, and comma operators), the order of evaluation of subexpressions and the order in which side effects take place are both unspecified."

Alok Save
  • 196,531
  • 48
  • 417
  • 525
3

The order of evaluation of arguments is unspecified. Compilers are free to implement it in any way they choose. Code like this will be brittle and unreliable.

Noufal Ibrahim
  • 69,212
  • 12
  • 131
  • 165