0

here is very simple c program:

int main()
{
    int i = 22;
    printf("%d %d %d",i,i++,i); 
    return 0;
}

The result is:

23 22 23

how exactly post increment is working here ?

dbush
  • 205,898
  • 23
  • 218
  • 273
Mohd Maaz
  • 267
  • 5
  • 20
  • 1
    Welcome to undefined behaviour! It's a wonderful land of mystery and misery. more here: https://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points – user4581301 Jul 15 '17 at 02:28
  • Good Point. OP wanted C. Rules are a bit different between C and C++, especially after C++11. – user4581301 Jul 15 '17 at 02:31

2 Answers2

2

You cannot test correctly in this context.

The order of parameter evaluation for function parameters is undefined, hence you will have platform-dependent results.

The way i++ works actually is by first returning the old value and later incrementing, but that is a bad test.

Germán Diago
  • 7,473
  • 1
  • 36
  • 59
1

Judging by the result you got, i++ is evaluated and returns the pre- incremented value. Then, the value of i, the return value of i++, and the value of i are passed to the print function.

This isn't ever something that you should rely on, as you may get different answers on different compilers or even with different optimization settings. As in the other answer, the order of parameter evaluation is undefined behavior.

Jonathan Olson
  • 1,166
  • 9
  • 19