CrazyEngineers
  • difference between post and pre increment op

    Updated: Oct 26, 2024
    Views: 1.1K
    what is diffrence amoung
    x=25;
    x=x++ + ++x;
    and
    cout<<x++ + ++x;
    and
    y=x++ + ++x;
    cout<<y;

    plz explain me the concept
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • chotaprogramer

    MemberApr 2, 2009

    i have same problem so is their any one who can solve our problem?
    Are you sure? This action cannot be undone.
    Cancel
  • chotaprogramer

    MemberApr 2, 2009

    its is fine but tell me if i do
    this
    y=x++ + ++x;
    and
    x=x++ + ++x;
    ten it gives me different outputs so tell me what is that concept behind it
    Are you sure? This action cannot be undone.
    Cancel
  • shalini_goel14

    MemberApr 2, 2009

    chotaprogramer
    its is fine but tell me if i do
    this
    y=x++ + ++x;
    and
    x=x++ + ++x;
    ten it gives me different outputs so tell me what is that concept behind it
    Hi
    An attempt to explain the concept for following scenario.

    x=25
    x= x++ + x++

    It will be taken as
    x =(x++) + (++x)
    LHS= x
    RHS= (x++) + (++x)

    Taking RHS
    (x++) will take first operand as 25 but increments it to 26 later because post increment operator. so first operand will be 25 but x will be 26

    (++x) will take x as 26 as calculated by first operand and before assigning this value to second operand, it will increment by 27 because it is pre increment operator so ultimately second operand will be 27

    SO now your RHS= First operand + second operand
    RHS= 25 +27 =52
    LHS=RHS (assignment operator)
    x=52
    so final value of x=52.

    Hope this may have cleared your doubt.Now you can apply same concept for any kind of scenario.

    PS: Anyone Please correct me if anything wrong.😀

    Thanks
    Are you sure? This action cannot be undone.
    Cancel
  • pradeep_agrawal

    MemberApr 3, 2009

    shalini_goel14
    x=25
    x= x++ + x++

    It will be taken as
    x =(x++) + (++x)
    LHS= x
    RHS= (x++) + (++x)

    ...

    so final value of x=52.

    When i compiled and executed the above code using gcc compiler the final result was x = 53. So i feel the logic described above is not correct.

    Also, why should the statement
    x = x++ + ++x;
    should evaluate to
    x = (x++) + (++x);
    and not to
    x = (x++++) + x;
    or
    x = x + (++++x);

    The reason it does not evaluate to
    x = (x++++) + x;
    or
    x = x + (++++x);
    is that in both the case the compiler will throw a error that lvalue is required because it does not have any lvalue on which it can store the result of second '++' operator (the '++' away from x in x++++ or ++++x).


    Now why it results in 53 and not 52? I feel below details should answer this.


    Before trying to understand the behavior we should have below understanding:
    - In case of ++ (pre-increment operator, ++x) in any statement, it always first increment the value and then used the new value in the statement.
    - In case of ++ (post-increment operator, x++) the value is first used in the statement and after completion of statement the value is incremented.


    So the above statements are evaluated as:

    Statement: y = x++ + ++x;

    1. x = 25
    2. Evaluating pre-increment (++x)
    Statement: y = x++ + 26
    and x = 26
    3. Evaluating post-increment (x++)
    Statement: y = 26 + 26
    and x = 26 (with x need to be incremented after execution of whole statement)
    4. Evaluating addition (+)
    Statement: y = 52
    and x = 26 (with x need to be incremented after execution of whole statement)
    5. Evaluating assignment
    y = 52
    x = 26 (with x need to be incremented after execution of whole statement)
    6. Final values
    y = 52
    x = 27


    Statement: x = x++ + ++x;

    1. x = 25
    2. Evaluating pre-increment (++x)
    Statement: x = x++ + 26
    and x = 26
    3. Evaluating post-increment (x++)
    Statement: x = 26 + 26
    and x = 26 (with x need to be incremented after execution of whole statement)
    4. Evaluating addition (+)
    Statement: x = 52
    and x = 26 (with x need to be incremented after execution of whole statement)
    5. Evaluating assignment
    x = 52 (with x need to be incremented after execution of whole statement)
    6. Final values
    x = 53


    Here we evaluated ++x first and x++ later (i.e., right portion of '+' first and left portion later). This behavior is compiler dependent. While evaluating any binary operator some compilers generate code to evaluate right portion first and some to evaluate left portion first (gcc do to evaluate right portion first). Other compilers may give different result. That's why it is recommended to avoid coding that's compiler dependent. This makes code easily portable.


    Let me know if any item need more clarification.

    -Pradeep
    Are you sure? This action cannot be undone.
    Cancel
  • Prasad Ajinkya

    MemberApr 3, 2009

    Very nicely done Pradeep 😀
    Are you sure? This action cannot be undone.
    Cancel
  • shalini_goel14

    MemberApr 3, 2009

    Hey Mr Pradeep

    Any ideas why the output is 52 in java and not 53? 😕
    Are you sure? This action cannot be undone.
    Cancel
  • pradeep_agrawal

    MemberApr 3, 2009

    shalini_goel14
    Any ideas why the output is 52 in java and not 53?
    I feel in Java the left portion of binary operator is evaluated first and then right portion. So x++ will be evaluated first and the ++x. So the computation will be as:

    Statement: x = x++ + ++x;

    1. x = 25
    2. Evaluating post-increment (x++)
    Statement: x = 25 + ++x
    and x = 25 (with x need to be incremented after execution of whole statement)
    3. Evaluating pre-increment (++x)
    Statement: x = 25 + 26
    and x = 26 (with x need to be incremented after execution of whole statement)
    4. Evaluating addition (+)
    Statement: x = 51
    and x = 26 (with x need to be incremented after execution of whole statement)
    5. Evaluating assignment
    x = 51 (with x need to be incremented after execution of whole statement)
    6. Final values
    x = 52

    This will result answer as 52.

    -Pradeep
    Are you sure? This action cannot be undone.
    Cancel
  • shalini_goel14

    MemberApr 3, 2009

    Thanks for clearing but are you sure these explanations are correct? 😀 I mean for the first time I saw such explanation of increment operators.

    Please confirm it once so that we poor people are not misguided.😔

    Thanks
    Are you sure? This action cannot be undone.
    Cancel
  • prettyprasu

    MemberSep 26, 2011

    Thank you for the explanation.. it was so useful to me.
    Are you sure? This action cannot be undone.
    Cancel
  • Richard Hanson

    MemberJul 3, 2015

    compiling with Dev-Cpp\MinGW64\bin\g++.exe

    int x {5} ;
    x = ++x ; // evaluates to 6
    a: ++x : increment x so x = 6
    b: assignment x = x ; so x = 6

    int x {5} ;
    x = x++ ; // evaluates to 5
    a: x++ : make a copy of x ( tmpx = x = 5 ) and then increment x so x = 6
    b: assignment x = tmpx ; so x reverts to 5, x = 5

    int x {5} ;
    x = ++x + ++x ; // evaluates to 14
    a: right ++x : increment x so x = 6
    b: left ++x : increment x so x = 7
    d: addition and assignment x = x + x that is x = 7 + 7 = 14

    int x {5} ;
    x = ++x + x++ ; // evaluates to 13
    a: ++x : increment x so x = 6
    b: x++ : make a copy of x ( tmpx = x = 6 ) and increment x so x = 7
    c: addition and assignment x = x + tmpx that is x = 7 + 6 = 13

    int x {5} ;
    x = x++ + ++x ; // evaluates to 12
    a: x++ : make a copy x (tmpx = x = 5 ) and increment x so x = 6;
    b: ++x : increment x so x = 7
    c: addition and assignment x = tmpx + x = 5 + 7 = 12;

    int x {5} ;
    x = x++ + x++ ; // evaluates to 11;
    a: right x++ : make a copy of x ( tempxr = x = 5 ) and increment x so x = 6
    b: left x++ : make a copy of x ( tempxl = x = 6 ) and increment x so x = 7
    c: addition and assignment x = tempxr + tempxl = 5 + 6 = 11

    int x {5} ;
    x = ++++x ; // evaluates to 7
    grouping right to left of ++++ so x = ++(++x) ;
    a: ++x : increment x so x = 6 and x is a left hand value a lvalue
    b: ++x : increment x so x = 7 and x is still a lvalue

    int x {5} ;
    x = x++++ ; // [Error] lvalue required as increment operand
    grouping left to right of ++++ so x = (x++)++ ;
    a: x++ : make a copy of x ( tmpx = x = 5 ) and increment x so x = 6
    we now have x = tmpx++ ; but tmpx is a temporary value and not a lvalue

    int x {5} ;
    x = ++x++ ; // [Error] lvalue required as increment operand
    postfix has higher precedence that prefix x = ++(x++) ;
    a: x++ : make a copy of x ( tmpx = x = 5 ) and increment x so x = 6
    we now have x = ++tmpx ; but tmpx is a temporary value and not a lvalue.

    int x {5} ;
    x = (++x)++ ; // evaluates to 6;
    a: ++x : increment x so x = 6
    b: x++ : save a copy of x ( tmpx = x = 6 ) and increment x so x = 7
    c: assignment x = tmpx = 6
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register