0

I've been trying to make sense of the functionalities of ++x and x++, but the minute I feel like I've figured it out, something new comes up that doesn't make sense with the previous rules.

So as far as I know, when using (++x),first the value of x increases by 1, and then the value of new (x) is assigned to the parenthesis (++x). On the other hand when using (x++), first the value of (x) is assigned to the parenthesis (x++), and then the value of x itself increases by 1.

For example I expect the output of this code:

int c=9;
int x=(++c)-c;
cout<<"c="<<c<<endl<<"x="<<x;

To be c=10 x=0 , and so it is.

Or also this code:

int c=9;
int x=(c++)-(++c);
cout<<"c="<<c<<endl<<"x="<<x;

with output of c=11 x=-2 , which also makes sense and is totally expected.

What doesn't make sense is that the output of this code:

int c=9;
int x=(++c)-(c++);
cout<<"c="<<c<<endl<<"x="<<x;

is c=11 x=1, which I expected it to be c=11 x=0. Because from left to right, first the value of c is increased by 1 (which means c=10), and then (++c) is replaced with the new value of c (which is 10). Moving on to the next parenthesis, first (c++) is replaced with the value of c (which is 10), and then the value of c itself is increased by 1, which means at the end we have c=11 and x=10-10=0.

The same issue with this code:

int c=9;
int x=(++c)*(c++);
cout<<"c="<<c<<endl<<"x="<<x;

generating this output c=11 x=110, which also doesn't make sense (I expected it to be c=11 x=100, with the exact same logic for the previous code)

And also this last one in which I have no idea what is going on:

int c=1;
int x=(++c)*(++c)*(++c);
cout<<"c="<<c<<endl<<"x="<<x;

with output of c=4 x=36 (which I expected to be c=4 x=24)! The funnier part is that if I change the second line to int x=2*(++c)*(++c)*(++c);, the output changes to c=4 x=48 !

I'd be grateful if anyone could help me with this.

mitrafrz
  • 51
  • 6
  • 7
    Welcome to the (not so) wonderful land of undefined behaviour where strange things happen. Note that some compilers will warn you about this error when you turn up the warning levels (which you should always do). – Lukas-T Apr 01 '21 at 12:15
  • @Xoxo.m.zf it's _undefined behaviour_ (google that term). This question comes at least once per week. The duplicate explains it. – Jabberwocky Apr 01 '21 at 12:22
  • 2
    Undefined Behaviour is undefined. This code can print any number, print "Hello world", print nothing, crash, format your C: drive, [make demons fly out of your nose](http://catb.org/jargon/html/N/nasal-demons.html) or do absolutely anything else. – Yksisarvinen Apr 01 '21 at 12:25
  • @Yksisarvinen so it's some kind of a bug? Aren't there any kind of rules (like priorities and stuff) that result in these unexpected calculations? – mitrafrz Apr 01 '21 at 12:36
  • @mitrafrz have a read of https://en.cppreference.com/w/cpp/language/ub There are a lot of traps in C++ and making yourself aware of them is part of the learning process. – Richard Critten Apr 01 '21 at 12:45
  • 2
    @mitrafrz No, it's not a bug. Read more here: https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior – πάντα ῥεῖ Apr 01 '21 at 12:46
  • 2
    Code like this `int x=(++c)*(++c)*(++c);` is BS anyway even if the bevaiour was not undefefined. Don't waste your time with reasoning undefined behaviour instead learn how to write readable efficient code. – Jabberwocky Apr 01 '21 at 12:53
  • In my opinion, most of C++ functions and syntaxes are BS anyway and I'd rather such post/pre-increments (x++ , ++x) didn't exist at all. But now that they do, I need to know how they work. Thanks for your help. @Jabberwocky – mitrafrz Apr 01 '21 at 13:03
  • @mitrafrz just don't use these kind of constructs. Period. Read the duplicate question and answers. And if you think C++ syntax etc.is BS, nobody prevents you from changing to another language. – Jabberwocky Apr 01 '21 at 13:06
  • @Jabberwocky firstly, thanks again for your advice; but I believe that no sane programmer actually gets "use" of such constructions (especially knowing they'll face some "undefined behaviour"). Secondly, I DID (gladly) change to Python a long time ago. And lastly, this website is created for people to ask their questions about whatever programming language, so that people who DO know the answer and DO care enough to READ the question, spend a tiny portion of their time to help and solve the confusion. The state of "being or not being BS" of a code, was never even considered in the first place. – mitrafrz Apr 01 '21 at 13:30
  • 1
    @mitrafrz you've been told multiple times that these constructions are _undefined behaviour_. That _is_ the answer. You cannot reason why these expression give a certain result. – Jabberwocky Apr 01 '21 at 13:32

0 Answers0