0
#include <stdio.h>
#include <stdlib.h>

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

Output: 2,3,0 I expect the output to be:2,2,0 Can someone explain how it actually gets evaluated

Manisha P
  • 111
  • 2
  • 9
  • Please elaborate your expectation. Why are you expecting "2,3,0"? I admit I expected "0,2,2". "0" because it is a postincrement, not done before reading the value. First "2" because the previous postincrement and a preincrement is effective when reading. Second "2" because, again, the postincrement did not get effective before reading. – Yunnosch Apr 01 '17 at 20:46
  • Why are you expecting "3,2,0"? Obviously is what I meant to ask. – Yunnosch Apr 01 '17 at 20:54
  • Possible duplicate of (http://stackoverflow.com/questions/376278/parameter-evaluation-order-before-a-function-calling-in-c) – Yunnosch Apr 01 '17 at 20:57
  • sorry, expected is 2,2,0. But what i get after compilation is 2,3,0.. and i don't know how is that – Manisha P Apr 02 '17 at 19:51
  • The question has been classified as "duplicate", i.e. the question and answer is already on Stackoverflow. The official other question discusses undefined behaviour. My recommendation is on why/that evaluation of parameters to functions is unpredictable (from compiler to compiler); quite interesting. Basically compilers can do anyting they like for optimisation. You must not attempt anything which requires prediction of parameter evaluation order. – Yunnosch Apr 02 '17 at 20:43

0 Answers0