-6

I wanted to know whether using the increment(++) or the decrement(--) operator on different variables in the same expression undefined behavior; Such as

int i=1,j=2;
int k=i++ + j++;

In the above code,the value of k is 3 in clang,GCC and in vc.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
  • 2
    why write such code? BTW - This "popular" question has been asked many times. Just do one thing at a time and you cannot go far wrong – Ed Heal Jan 03 '15 at 09:48
  • @EdHeal This question is asked many times but people just don't know to look on 'sequence points' as term when looking up this question – hetepeperfan Jan 03 '15 at 10:01
  • 1
    @hetepeperfan One can find all the explanations one needs by searching “increment undefined behavior”, all words that the OP definitely knows. – Pascal Cuoq Jan 03 '15 at 12:04

1 Answers1

5

Does using the ++ or — operator on different variables in the same expression invoke UB?

No. It will not invoke undefined behavior. You can use ++ or -- any number of times in a expression on different objects. In this case each variable is modifying only once within two sequence points.

haccks
  • 104,019
  • 25
  • 176
  • 264