Thread: Two pre increment in one expression

  1. #1
    Registered User
    Join Date
    Sep 2019
    Posts
    4

    Two pre increment in one expression

    Dear all
    Please see this part of code:
    int p=0,q=0;
    q= ++p + ++p;

    It is expected for q to be 3 after the expression, 1+2..
    But it becomes 4,2+2..
    Could not understand the background logic of the compiler
    If you say :
    q= ++p + 0 + ++p;
    Or:
    q= ++p + 1 + ++p;
    In both forms q becomes 4..
    Any one has any idea about the logic of this calculation?
    Thank you all

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,527
    It's not expected. That is undefined behaviour so it can give any answer it wants to

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,539
    This is an example of how UB can mess with your mind / code / life.

    Same code, different compilers, different answers.
    It's undefined
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2019
    Location
    Inside a Singularity
    Posts
    194
    Why are you doing this anyway? I'm guessing it came in a school test that wants to check if one has understood pre-increment and post-increment.
    I've learnt by being on this forum that this results in Undefined Behaviour and there's a Wiki doc you can refer to about it.

    My teacher at school says "For such kind of questions, perform all evaluations first and then add up the results." She means to say:
    For q = ++p + ++p, do the ++p's first. That makes p=2. Now, you're adding the value of p which gives you 4. But this obviously doesn't make sense. Have a look at Salem's reply on the link he shared. With different compiler settings, you can yield different results. I've got both answers 3 and 4 after changing my compiler settings on C::B but neither of them can actually be considered the correct answer as the behaviour itself is "undefined".

    If you do something like P = ++P, then too the result is undefined even though the compiler happily compiles it without any error as you are basically doing a double assignment of the same variable (P = P = P+1).

    For expressions like "cout << ++p << p << p++" (p is initially 0), you'd expect a result "111" but I've learnt that such expressions are evaluated right to left yielding a theoretical result "012". I've gotten different results on Borland and GCC which makes this undefined behaviour too I guess (I'm not sure, but is this too undefined behaviour @Salem @Hodor?)

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,539
    Well you need to know what a sequence point is, and the "object modified at most once" rule.
    Question 3.8
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post Increment an Pre Increment operators in c++
    By anil_ in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2011, 08:27 PM
  2. initializer expression list treated as compound expression
    By karthikeyanvisu in forum C Programming
    Replies: 7
    Last Post: 02-26-2011, 05:19 PM
  3. Replies: 2
    Last Post: 11-25-2009, 07:38 AM
  4. Replies: 6
    Last Post: 11-11-2009, 02:27 PM
  5. Post increment and pre increment help
    By noob2c in forum C++ Programming
    Replies: 5
    Last Post: 08-05-2003, 03:03 AM

Tags for this Thread