-3

How to interpret these pre increment operators?
Pre increment operators have right to left associativity, so the right most i will be incremented or all the i's will be incremented once?

main()
 {
    int i=3,j;
    j=++i*++i*++i;
    printf("%d",j);
 }

Answer is 216.

  • 1
    No. In that question no assignment statement has the same operator. It has one pre and one post . Correct me if i am wrong. – PallaviBatra Aug 13 '13 at 12:49
  • This question is a duplicate with the second question, sequence points. It does not matter if it is the same operator or not, the behavior is still undocumented and thus undefined. There is nothing new to learn from this question, except if you can bring some new information into the question. The correct answer is that it is not documented how all the `++` operator will execute and in which order. Though order between the `++` here does not matter, the order with the rest of the expression does, but this is unfortunately undefined. – Lasse V. Karlsen Aug 13 '13 at 13:27

3 Answers3

0

According to the c11 standard, §6.5 par. 2 of the working draft, expressions like these are indeed undefined.

collapsar
  • 17,010
  • 4
  • 35
  • 61
-1

++i increments i before the operation.

You code is equivalent to :

int i=3,j;
j=(i+1)*(i+2)*(i+3);
i++;i++;i++;
printf("%d",j);

This will output 4*5*6=120

edi9999
  • 19,701
  • 13
  • 88
  • 127
  • I actually tried - I expected to get possibly varying answer depending on compiler options, due to it being undefined behaviour - and consistently got *150* (5*5*6) on OpenSuSE 12.1 with latest `gcc`. – LSerni Aug 13 '13 at 12:56
  • Undefined does not mean "random", it just means "undocumented". It may be that *that* compiler always produce code that returns 150. The problem is how strongly you want to rely on that. – Lasse V. Karlsen Aug 13 '13 at 13:23
-1

Answer is 150

Is it equal to (++i * ++i) * ++i, first ++i increments i (i=4), second ++i increments i (i=5) but it is the same i so 5*5 = 25. Finally 25 * ++i = 150

Polymorphism
  • 249
  • 1
  • 9