Results 1 to 11 of 11

Thread: confuse

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2003
    Posts
    62

    confuse

    #include <iostream>
    using std::cout;

    int main()
    {
    int a;

    int b = 7;

    a = ++b + ++b - --b + --b;
    cout << a;
    return 0 ;
    }


    y the output in c++ is 17??

    should be 16 right?? anyone may clarify me about this???

    thanks in advance ...hehe
    Last edited by Little; Mar 28th, 2003 at 08:14 AM.

  2. #2
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986

    Re: confuse

    Please always include your code between code tags, makes it easier to read...

    here's your code broken down so you can see why it's 16:

    Code:
    int main()
    {
    int a;
    int b = 7;
     
    a = ++b; //a = 8, b = 8
    a += ++b; //a = 17, b = 9
    a -= --b; //a = 9, b = 8
    a += --b; //a = 16, b = 7
    
    cout << a;
    return 0;
    }
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2003
    Posts
    62
    Sorry...i mean the output is not 16...

    i use vc++ to compile it...

    the output is 17....

    that's y i dun understand...
    Last edited by Little; Mar 28th, 2003 at 08:09 AM.

  4. #4
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    The output was 16 for me?

    Try to use the code below, debug it and step trought the code with f10, and try to watch your window and see what happens.
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2003
    Posts
    62
    yaya......
    if i use the code u give me...
    it absolutely will give 16....
    but if u put them in one line ....the output will be 17........

    i understand ur code very well...
    i just dun know y if i put them all in one line, the output will not be 16...

    if follow the precedence and associative rules.....it should be 16 (if put in one line)...

    i just dun know y???

    u try to use the code that i post there...

    izzit there is short circuit code....it just execute the first two prefix code.....and the compiler find the last two prefix code is equal to 0...so it won't execute it....and the output is 17 instead of 16???

    vb novice

  6. #6
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Well I am getting 14 on VC7 which is what it should be and here is why, it does the all the ++b before the add and sub so you code looks like this:

    a = ++b + ++b - --b + --b;
    a = 8 + ++b - --b + --b;
    a = 9 + 9 - --b + --b;
    a = 8 + 8 - 8 + --b;
    a = 7 + 7 - 7 + 7;
    a = 14 - 7 + 7;
    a = 7 + 7;
    a = 14;
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  7. #7

    Thread Starter
    Member
    Join Date
    Jan 2003
    Posts
    62
    But y visual c++6....the output is 17????

    i try many calculation alredi...it is not possible to get that value..

    i just wonder... hehe...

  8. #8
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    How ODD!!
    Well here is what I can figure:

    when I step through b = 7 at the end of it so
    a = ++b + ++b - --b + --b;
    a = ++b + ++b - --b + 7;

    The first ++b must be 8
    a = 8 + ++b - --b + 7;

    if I take a guess and say the remaining --b would be 8 so the second --b = 7

    a = 8 + ++b - 8 + 7;

    That would make the last ++b 10 then. Which makes no sense. So you got me, I am going to sick with VC7.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  9. #9

    Thread Starter
    Member
    Join Date
    Jan 2003
    Posts
    62
    yaya...it makes no sense....

    i am quite confuse about this....

    how the code being execute....sigh....


  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Never use more than one increment/decrement operator on the same variable on one line. It always causes confusion, caused some pointer code of mine not to work for example.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    The order they are resolved in is *not* guaranteed, on any compiler. I believe that all the standard requires is that they get evaluated "some time".

    Jim can most likely expand on this better than I can...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width