bug or what?

int a=3;
int ans=a+a++;
output: 7
-------------------------------------
int a=3;
int ans=a+a+a++;
output: 9
-------------------------------------
int a=4
int ans=a+a+a+a++;
output: 12

why??
a+a++
What does that do? The C++ programming language doesn't specify what it should do. It can do whatever it likes.

a+a+a++
What does that do? The C++ programming language doesn't specify what it should do. It can do whatever it likes.

a+a+a+a++
What does that do? The C++ programming language doesn't specify what it should do. It can do whatever it likes.

Here's some reading: https://stackoverflow.com/questions/949433/why-are-these-constructs-using-undefined-behavior

Basically, you're writing code for which there is no defined behaviour. Don't do that. Don't mix increments and decrements of a variable with other uses of that same variable.
> why??

Undefined behaviour.
See: http://en.cppreference.com/w/cpp/language/eval_order

Enable warnings and the compiler would alert you about this:
http://coliru.stacked-crooked.com/a/54bbde16bd60436f

Topic archived. No new replies allowed.