-6
#include<stdio.h>
int main()
{
    int i = 2;
    printf("\n %d %d %d \n",--i,i--,i);    //   0 2 2
    return 0;
}

The output prints 0 2 0 and not 0 2 2.I couldn't understand, as I assumed that the printf() evaluates from right to left.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Angus
  • 12,133
  • 29
  • 96
  • 151

2 Answers2

4

Your code exhibits Unspecified behaviour. As per c99 standard document, chapter 6.5.2.2, paragraph 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.

Again, this shows undefined behaviour, because, i is getting modified more than once between two sequence points. As per chapter 6.5 paragraph 2:

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
-1

printf evaluates nothing. It is the compiler that evaluates arguments of a function (that is it is the compiler that generates the object code) and the order in which the arguments will be evaluated in C is unspecified. So the compiler may evaluates them in any order.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    That is about the order of evaluation. But you forgot about the side-effects "modifying i multiple times without an intervening sequence point" – wildplasser Jan 10 '15 at 15:43
  • @wildplasser I have forgot nothing because the side effects are secondary relative to the order of evaluation of arguments. – Vlad from Moscow Jan 10 '15 at 15:45
  • 1
    @VladfromMoscow: What total nonsense. Sequencing issues render everything else irrelevant. – Lightness Races in Orbit Jan 10 '15 at 15:46
  • 2
    @Lightness Races in Orbit Total nonsense is what you have written because if the order of evaluation of arguments is unspecified then there is no sense to speak about sequantial points. – Vlad from Moscow Jan 10 '15 at 15:48
  • @VladfromMoscow; I agreed with you. – haccks Jan 10 '15 at 15:48
  • @wildplasser; C11 changes that rule. Now its all about *sequencing*. – haccks Jan 10 '15 at 15:49
  • 1
    No, the order of side-effects is completely unrelated to the order of evaluation. Only sequence points matter wrt side effects. – wildplasser Jan 10 '15 at 15:50
  • @wildplasser; **C11: 6.5 Expressions:** *If a side effect on a scalar object is **unsequenced** relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings* – haccks Jan 10 '15 at 15:51
  • 1
    @wildplasser It is evident that you are saying a nonsense. What will the sequence point be if the order is unspecified?! – Vlad from Moscow Jan 10 '15 at 15:54
  • The only relevant sequence point is the calling of the function. Once the code "inside" the function is executed, all side effects should have taken place. (in this case: two "conflicting" side effects) – wildplasser Jan 10 '15 at 15:58
  • @wildplasser; Did you read the quote from standard in my last comment? – haccks Jan 10 '15 at 16:08
  • Yes. In this case there is no difference, because i's value is being modified twice (and there is no order implied by the order of evaluation) – wildplasser Jan 10 '15 at 16:13