-8

What will be the output of code

int a=3,b=4;
++a*=++b;
cout<<a<<endl;

compiler shows a=20 output.How precedence and operator associativity is being used here?

What I understand is:

first b on the left of ++a*=++b; is incremented us its unary operator then comes the turn of *= so expression becomes ++a = a * ++b; as a=3 and b=5 now so it becomes 15 then 15 is assigned to a and incremented.Finally getting 16 but compiler gives 20

  • 2
    Your understanding is wrong. Your code modifies `a` *twice* without an intervening *sequence point*. Therefore your code has *undefined behaviour*. See cHao's link for a detailed explanation. – john Apr 22 '13 at 21:14
  • It's undefined behavior due to lack of sequence points. It's plausible that 4*5 = 20; however, you don't know what the result of the ++a will be so it's undefined. –  Apr 22 '13 at 21:18
  • @Infinity - Please check out the Beginners section of [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). You *definitely* need a good book to read. – Bo Persson Apr 22 '13 at 21:46

1 Answers1

1

In your particular case on your particular compiler, it seems that first a is incremented to 4 and b is incremented to 5, then a *= b executes and a becomes 20 (4*5). However other compiler could give different result because it is not a defined behaviour as people mentioned in comments

cHao
  • 84,970
  • 20
  • 145
  • 172
Martinsos
  • 1,663
  • 15
  • 31
  • That's a plausible explanation of the observed behavior. In general, though, the behavior is undefined. – Keith Thompson Apr 22 '13 at 21:17
  • It's explicitly stated in the standards for both C and C++ that the behavior is undefined when you modify the same object twice without a sequence point in between. That the math happens to work out means nothing; it could operate totally different with another compiler, or between runs of the same program...or, technically, set your computer on fire. – cHao Apr 22 '13 at 21:18
  • I thought that ++a and ++b will always happen before multiplication, is that wrong? – Martinsos Apr 22 '13 at 21:18
  • 1
    @Martinsos When you have undefined behaviour there is nothing that 'always happens'. The code is not valid C++, there no expected behaviour from this code. `c = ++a * ++b` would work as you say, but the code above is not like that because a is modified twice. That's the problem. – john Apr 22 '13 at 21:21
  • I think you are right as associativity of `*=` is lesser than ++a so `++a` will be solved earlier. – Infinity Signal Apr 22 '13 at 21:22
  • 1
    @InfinitySignal It's undefined behaviour, your understanding is incomplete. Associativity has nothing to do with it. – john Apr 22 '13 at 21:23
  • Should I delete my answer hm? I like your comments so I don't like idea of deleting it – Martinsos Apr 22 '13 at 21:27
  • 1
    @Martinsos Your answer looks fine to me now, I removed my down vote. – john Apr 22 '13 at 21:28
  • 1
    I added a bit to the wording; wanted to stress up front that the results the OP mentions are specific to the OP. – cHao Apr 22 '13 at 21:32