postfix prefix of c++.... i am unable to solve the problem... please kindly help me t

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shree17
    New Member
    • Jun 2013
    • 1

    postfix prefix of c++.... i am unable to solve the problem... please kindly help me t

    i am not able to solve the following-

    M= (++N)+(++P++)
    N= ++P--
    P= --M++
    M= ++P
    N= --M
    P= ++N
    Print M+N+P=?
    Please kindly help me to solve it...
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You are allowed to change the value of a variable only once in a statement. Otherwise, the behavior is indeterminate.

    So this code:
    Code:
    M= (++N)+(++P++)
    will give you different answers on different compilers.

    Compilers are allowed to evaluate the variables in any order, left to right or right to left before making any decisions using those variables. As a result, a variable that changes within a statement has an indeterminate value.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Additionally these 2 lines will not even compile

      N= ++P--
      P= --M++

      because ++ and -- (prefix or postfix) have to operate on an lvalue (basically a value you can assign to) but in these lines the prefix operators act on the output of the postfix operators and these are not lvalues, you can't assign to them as they have no memory address.

      Comment

      Working...