the result of the following expressions
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: the result of the following expressions

Hybrid View

  1. #1
    Join Date
    Nov 2012
    Posts
    2

    the result of the following expressions

    hello,


    any can explain me the which a am going to calculate first of this

    int α=1 double d=1.0

    a = 5 + 5 * 2 % a--;


    this

    d + = 1.5 * 3 + (++d);

    this
    d - = 1.5 * 3 + d++;


    i found wrong results i want to know the mistake how can i calculate this without c++
    thanks a lot!!!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,434

    Re: the result of the following expressions

    Quote Originally Posted by D3xt3R View Post
    hello,


    any can explain me the which a am going to calculate first of this

    int α=1 double d=1.0
    Code:
        a = 5 + 5 * 2 % a--;               
        ...
        d + = 1.5 * 3 + (++d);
        ...
        d - = 1.5 * 3 + d++;
    Let me ask you, what did you expect the answer to be for any of these expressions?

    The problem is that there is no answer. There is no guarantee when d++ or ++d will be executed in that block of code. There is something called a "sequence point" in C++, and I suggest you do a google search for that term.

    http://en.wikipedia.org/wiki/Sequence_point

    Instead of code that looks like what you wrote, what's wrong with just doing things simple? Evaluate each step, one at a time instead of trying to erroneously cram everything into one statement.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Nov 2012
    Posts
    2

    Re: the result of the following expressions

    i know the sequence but i can figure out these examples i run it in c++ and the results are different from those i made on the paper
    that the reason i ask or help i anyone can explain how can calculate them
    thanks

  4. #4
    Join Date
    Apr 1999
    Posts
    27,434

    Re: the result of the following expressions

    Quote Originally Posted by D3xt3R View Post
    i know the sequence
    No you don't know the sequence, and neither does anyone else.

    Again, there is no guarantee how C++ will calculate those lines. There is no "sequence" that is guaranteed to occur.
    i run it in c++ and the results are different from those i made on the paper
    So tell us, what did you do "on paper"? Did you just assume that "++d", "+=", "-=", and "d++" were supposed to always do things in a certain order if they appear in a single line of code? If you did assume this, then you're wrong.

    Again, read the link I gave you. Simply put, if you have prefix, postfix, addition, subtraction on one line of code like that, then the behaviour of the program is undefined.
    that the reason i ask or help i anyone can explain how can calculate them
    No one can calculate them, since the sequence is ambiguous.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,976

    Re: the result of the following expressions

    Paul already answered this. See the sequence point link. Those expressions do not have a guaranteed answer that will be the same on every C++ compiler you run it on.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center