Prob in opreator?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AnagJohari
    New Member
    • May 2010
    • 96

    Prob in opreator?

    Code:
    void main()
    {
    int i=2;
    printf("%d %d",++i,++i)
    getch();
    }
    The ans of this program is 4 3
    but in my opinion the ans should be 3 3
    plz explain why ans 4 3 is comming........ ..
    Last edited by Banfa; May 14 '10, 08:58 AM. Reason: The / is [/CODE] is this one / NOT this one \
  • hype261
    New Member
    • Apr 2010
    • 207

    #2
    The reason why the answer is 4 3 is because C evaluates arguments from right to left. So...

    Code:
    printf("%d %d, ++i; ++i) <--- second ++i is evaluated first
    Also you are using the preincrement operator(++i) which means you increment i before it is evaluated.

    if you did

    Code:
    printf("%d %d, i++, i++)
    your answer would be 3 2

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      C does not evaluate arguments from right to left.
      In fact, the order of evaluation is explicitly labeled implementation-defined. That means C is free to use whatever order it feels like. You might get one result now, but a different one tomorrow. You should consider the output of this program to be unpredictable.

      Comment

      • hype261
        New Member
        • Apr 2010
        • 207

        #4
        Sorry for the miss information

        Sorry for the miss information. Guess I missed that little tid bit of knowledge in portability.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          It's nothing to do with portability. Instead, the rule is that since the order of evaluation is undefined then referring to an variable twice can produce indeterminant results of one or more of the references change the value of the variable.

          This is OK:

          Code:
          if (++i)
          etc...
          but this is not:

          Code:
          if (++i  == --i)
          etc...

          Comment

          • hype261
            New Member
            • Apr 2010
            • 207

            #6
            Originally posted by weaknessforcats
            It's nothing to do with portability. Instead, the rule is that since the order of evaluation is undefined then referring to an variable twice can produce indeterminant results of one or more of the references change the value of the variable.

            This is OK:

            Code:
            if (++i)
            etc...
            but this is not:

            Code:
            if (++i  == --i)
            etc...
            So are you saying that the compilier itself will not evaluate the function the same way everytime?

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              Originally posted by hype261
              So are you saying that the compilier itself will not evaluate the function the same way everytime?
              The compiler is not required to evaluate the operators the same way every time, even if you don't change the expression. Apparently inconsequential changes to the expression or the command line switches might cause the optimizer to do the preincrements in a different order.

              Comment

              • hype261
                New Member
                • Apr 2010
                • 207

                #8
                Originally posted by donbock
                The compiler is not required to evaluate the operators the same way every time, even if you don't change the expression. Apparently inconsequential changes to the expression or the command line switches might cause the optimizer to do the preincrements in a different order.
                Good to know, thank you.

                Comment

                • AnagJohari
                  New Member
                  • May 2010
                  • 96

                  #9
                  Originally posted by hype261
                  Good to know, thank you.
                  But in a book the ans of this expression is given 4 3 not give the ans that "it will varry from one compiller to othetr compiller....."

                  other thing may i know is that if i write
                  i=2;
                  i=++i
                  then the value of i changed or produce an error in a program

                  Comment

                  • donbock
                    Recognized Expert Top Contributor
                    • Mar 2008
                    • 2427

                    #10
                    Originally posted by AnagJohari
                    But in a book the ans of this expression is given 4 3 not give the ans that "it will varry from one compiller to othetr compiller....."

                    other thing may i know is that if i write
                    i=2;
                    i=++i
                    then the value of i changed or produce an error in a program
                    Code:
                    printf( "%d %d", ++i, ++i);
                    What book? Are you certain the example in the book was precisely this?



                    Code:
                    i = 2;
                    i = ++i;
                    Nothing mysterious here. The value of i should end up as 3. Are you saying the compiler gave you an error for these instructions?

                    Comment

                    • AnagJohari
                      New Member
                      • May 2010
                      • 96

                      #11
                      Originally posted by donbock
                      Code:
                      printf( "%d %d", ++i, ++i);
                      What book? Are you certain the example in the book was precisely this?



                      Code:
                      i = 2;
                      i = ++i;
                      Nothing mysterious here. The value of i should end up as 3. Are you saying the compiler gave you an error for these instructions?
                      i m actually finding the concept of this finding the output of an programe
                      there are four options ,,, one of the option is 4 3
                      & second one is ans vary from one compiller to other compiller..
                      in a book ans is given 4 3
                      but u suggest the ans vary from one compiller to other
                      so i m in coonfused....

                      Comment

                      • weaknessforcats
                        Recognized Expert Expert
                        • Mar 2007
                        • 9214

                        #12
                        All you need to remember is that changing a variable more than once in the same statement produces indeterminate results.

                        Indeterminate means you can't predict the result. Maybe it works, maybe it doesn't.

                        BAD:

                        Code:
                        printf("%d%d\n", ++i, --i);
                        GOOD:

                        Code:
                        printf("%d", ++i);
                        printf("%d\n", --i);

                        Comment

                        Working...