post fix and pre fix operator simple puzzle

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anirudh
    New Member
    • Oct 2006
    • 2

    post fix and pre fix operator simple puzzle

    hello everyone..
    i have come across this very simple puzzle ..
    consider
    x=++a * --a * a++;
    where the value of a=6..
    what should be the value of X.
    please give u r answers as well as u r solutions.. please.
    thanks in advance.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    X could be anything and in fact and program invoking this code could do anything because this line invokes undefined behaviour because you have read and written the variable a more than once between sequence points.

    Comment

    • jessy
      New Member
      • Oct 2006
      • 106

      #3
      what a very nice puzzle .........

      i'm confused !! why is --a acting this way

      i've tried a=2

      the result should be X=3*1*2 = 6 but the result is 12 which means that he didn't decrement a so why the hell is that !!!!!!

      plz answer me

      Comment

      • naeemulhaq
        New Member
        • Oct 2006
        • 3

        #4
        Its simple.
        Pre-fix operations are executed before the statement in which they occur and post-fix after that statement.

        initially a = 6;
        a = 6;
        //++a makes a = 7
        //--a makes a = 6

        //so here you are doing: x = a*a*a
        x= ( (++a * --a) * a++);

        //here a becomes 7 due to a++

        This program in calculating cube of a. I'm using Dev-C++.

        Comment

        • jessy
          New Member
          • Oct 2006
          • 106

          #5
          could you please tell me how --a makes a =6 shouldn't it supposed to be 5

          i tried a program that accepts an int from the user ( a) and output --a the result was 5 not 6 .......??
          '
          so if a=2 (++A*--A*A++)
          = 3*1*2=6

          and also i need to know what's DEV_c ?? that you are using ???

          Comment

          • vninja
            New Member
            • Oct 2006
            • 40

            #6
            it should be 5 but since its the same variable consider:

            x=++a*--a*a++;
            since your incrimenting and decrimenting it becomes the original value

            prstatement
            a=6

            first (++a)
            a=7
            secont pre(--a)
            a=6
            no more pre means do the statement
            x=6*6*6
            x=216
            post fix (a++)
            a = 7

            Comment

            • vninja
              New Member
              • Oct 2006
              • 40

              #7
              it might depend on the program/compiler as well

              for the two
              the way it would be 12 and not 6 would be because it incriments and decrements the prefix before it uses the value and not the statement; ie

              a=2

              x=++a*--a*a++
              it would incriment a to 3 first
              then a =3 and not 2!!!!
              producing 3*2
              then since we postfix a is changed after use
              making the expression x=3*2*2=12 and not x=3*1*2

              Comment

              Working...